Permalink
Browse files

renames: cond->, as->, some->

  • Loading branch information...
1 parent 1588ff3 commit 372f03e2fa63ff7c3544be82d85e8943e85e640b @richhickey richhickey committed Dec 2, 2012
Showing with 19 additions and 16 deletions.
  1. +19 −16 src/clj/clojure/core.clj
View
@@ -6725,22 +6725,25 @@
{:added "1.3"}
[^clojure.lang.IPending x] (.isRealized x))
-(defmacro test->
+(defmacro cond->
"Takes an expression and a set of test/form pairs. Threads expr (via ->)
- through each form for which the corresponding test expression (not threaded) is true."
+ through each form for which the corresponding test
+ expression is true. Note that, unlike cond branching, cond-> threading does
+ not short circuit after the first true test expression."
{:added "1.5"}
- [expr
- & clauses]
+ [expr & clauses]
(assert (even? (count clauses)))
(let [g (gensym)
pstep (fn [[test step]] `(if ~test (-> ~g ~step) ~g))]
`(let [~g ~expr
~@(interleave (repeat g) (map pstep (partition 2 clauses)))]
~g)))
-(defmacro test->>
+(defmacro cond->>
"Takes an expression and a set of test/form pairs. Threads expr (via ->>)
- through each form for which the corresponding test expression (not threaded) is true."
+ through each form for which the corresponding test expression
+ is true. Note that, unlike cond branching, cond->> threading does not short circuit
+ after the first true test expression."
{:added "1.5"}
[expr & clauses]
(assert (even? (count clauses)))
@@ -6750,34 +6753,34 @@
~@(interleave (repeat g) (map pstep (partition 2 clauses)))]
~g)))
-(defmacro let->
+(defmacro as->
"Binds name to expr, evaluates the first form in the lexical context
of that binding, then binds name to that result, repeating for each
- successive form"
+ successive form, returning the result of the last form."
{:added "1.5"}
[expr name & forms]
`(let [~name ~expr
~@(interleave (repeat name) forms)]
~name))
-(defmacro when->
- "When expr is logical true, threads it into the first form (via ->),
- and when that result is logical true, through the next etc"
+(defmacro some->
+ "When expr is not nil, threads it into the first form (via ->),
+ and when that result is not nil, through the next etc"
{:added "1.5"}
[expr & forms]
(let [g (gensym)
- pstep (fn [step] `(when ~g (-> ~g ~step)))]
+ pstep (fn [step] `(if (nil? ~g) nil (-> ~g ~step)))]
`(let [~g ~expr
~@(interleave (repeat g) (map pstep forms))]
~g)))
-(defmacro when->>
- "When expr is logical true, threads it into the first form (via ->>),
- and when that result is logical true, through the next etc"
+(defmacro some->>
+ "When expr is not nil, threads it into the first form (via ->>),
+ and when that result is not nil, through the next etc"
{:added "1.5"}
[expr & forms]
(let [g (gensym)
- pstep (fn [step] `(when ~g (->> ~g ~step)))]
+ pstep (fn [step] `(if (nil? ~g) nil (->> ~g ~step)))]
`(let [~g ~expr
~@(interleave (repeat g) (map pstep forms))]
~g)))

0 comments on commit 372f03e

Please sign in to comment.