Harpoons

Harpoons is a Clojure and Clojurescript library providing a set of new threading macros that complement and compose well with the existing core threading macros.

Starting threading context

Start a normal threading context:

(-> expr
  threaded-form1threaded-formn)
(->> expr
  threaded-form1threaded-formn)
(-<> expr
  threaded-form1threaded-formn)

Start a short-circuiting threading context that exits at the first nil value:

(some-> expr
  threaded-form1threaded-formn)
(some->> expr
  threaded-form1threaded-formn)
(some-<> expr
  threaded-form1threaded-formn)

Start a short-circuiting threading context that exits at the first non-nil value:

(non-nil-> expr
  threaded-form1threaded-formn)
(non-nil->> expr
  threaded-form1threaded-formn)
(non-nil-<> expr
  threaded-form1threaded-formn)

Start a conditional threading context:

(cond-> expr
  test1 threaded-form1testn threaded-formn)
(cond->> expr
  test1 threaded-form1testn threaded-formn)
(cond-<> expr
  test1 threaded-form1testn threaded-formn)

Modifying threading context

Convert the enclosing threading context into a corresponding short-circuiting context that exits at the first nil value:

(>-some
  threaded-form1threaded-formn)
(>>-some
  threaded-form1threaded-formn)
(<>-some
  threaded-form1threaded-formn)

Convert the enclosing threading context into a corresponding short-circuiting context that exits at the first non-nil value:

(>-non-nil
  threaded-form1threaded-formn)
(>>-non-nil
  threaded-form1threaded-formn)
(<>-non-nil
  threaded-form1threaded-formn)

Convert the enclosing threading context into a corresponding conditional threading context:

(>-cond
  test1 threaded-form1testn threaded-formn)
(>>-cond
  test1 threaded-form1testn threaded-formn)
(<>-cond
  test1 threaded-form1testn threaded-formn)

Bind the threaded value to the symbols in binding-form destructuring the value as necessary:

(>-bind binding-form
  threaded-form1threaded-formn)
(>>-bind binding-form
  threaded-form1threaded-formn)
(<>-bind binding-form
  threaded-form1threaded-formn)

Evaluating body within threading context

Bind the threaded value to the symbols in binding-form destructuring the value as necessary. Evaluate the body forms returning the value of the last form back to the enclosing threading context:

(>-let binding-form
  body-form1body-formn)
(>>-let binding-form
  body-form1body-formn)
(<>-let binding-form
  body-form1body-formn)

Evaluate the body forms returning the value of the last form back to the enclosing threading context. The threaded value is bount to the <> symbol throughout the scope of the body:

(>-do
  body-form1body-formn)
(>>-do
  body-form1body-formn)
(<>-do
  body-form1body-formn)

Evaluate the body forms for the side-effects only without altering the threaded value in the enclosing threading context. The threaded value is bound to the <> symbol throughout the scope of the body:

(>-fx!
  body-form1body-formn)
(>>-fx!
  body-form1body-formn)
(<>-fx!
  body-form1body-formn)

Bridging between different threading contexts

Bridge to an inner > threading context. Note that when bridging from an outer <> context the value threaded in the context remain bound to the <> symbol and, thus, is accessible throughout the scope of the bridging form:

(>>->
  threaded-form1threaded-formn)
(<>->
  threaded-form1threaded-formn)

Bridge to an inner >> threading context. Note that when bridging from an outer <> context the value threaded in the context remain bound to the <> symbol and, thus, is accessible throughout the scope of the bridging form:

(>->>
  threaded-form1threaded-formn)
(<>->>
  threaded-form1threaded-formn)

Bridge to an inner <> threading context.

(>-<>
  threaded-form1threaded-formn)
(>>-<>
  threaded-form1threaded-formn)