summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2009-08-29 13:54:47 -0400
committerRich Hickey <richhickey@gmail.com>2009-08-29 13:54:47 -0400
commit946e7af6dfde90eb4392d055be1ebd4bebf07ee8 (patch)
treee94e6684371aa0fa151e68c0a46446d902561dbd /src
parent5ca86e045bd5742566c647dcc46b8e546a6a7c06 (diff)
added juxt, juxt[aposes] fns and retuns a fn that returns vector of their results
Diffstat (limited to 'src')
-rw-r--r--src/clj/clojure/core.clj37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 1666d4d1..714ccff3 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -1546,6 +1546,43 @@
(recur ((first fs) ret) (next fs))
ret))))))
+(defn juxt
+ "Alpha - name subject to change.
+ Takes a set of functions and returns a fn that is the juxtaposition
+ of those fns. The returned fn takes a variable number of args, and
+ returns a vector containing the result of applying each fn to the
+ args (left-to-right).
+ ((juxt a b c) x) => [(a x) (b x) (c x)]"
+ ([f]
+ (fn
+ ([] [(f)])
+ ([x] [(f x)])
+ ([x y] [(f x y)])
+ ([x y z] [(f x y z)])
+ ([x y z & args] [(apply f x y z args)])))
+ ([f g]
+ (fn
+ ([] [(f) (g)])
+ ([x] [(f x) (g x)])
+ ([x y] [(f x y) (g x y)])
+ ([x y z] [(f x y z) (g x y z)])
+ ([x y z & args] [(apply f x y z args) (apply g x y z args)])))
+ ([f g h]
+ (fn
+ ([] [(f) (g) (h)])
+ ([x] [(f x) (g x) (h x)])
+ ([x y] [(f x y) (g x y) (h x y)])
+ ([x y z] [(f x y z) (g x y z) (g x y z)])
+ ([x y z & args] [(apply f x y z args) (apply g x y z args) (apply h x y z args)])))
+ ([f g h & fs]
+ (let [fs (list* f g h fs)]
+ (fn
+ ([] (reduce #(conj %1 (%2)) [] fs))
+ ([x] (reduce #(conj %1 (%2 x)) [] fs))
+ ([x y] (reduce #(conj %1 (%2 x y)) [] fs))
+ ([x y z] (reduce #(conj %1 (%2 x y z)) [] fs))
+ ([x y z & args] (reduce #(conj %1 (apply %2 x y z args)) [] fs))))))
+
(defn partial
"Takes a function f and fewer than the normal arguments to f, and
returns a fn that takes a variable number of additional args. When