diff options
author | Rich Hickey <richhickey@gmail.com> | 2009-08-29 13:54:47 -0400 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2009-08-29 13:54:47 -0400 |
commit | 946e7af6dfde90eb4392d055be1ebd4bebf07ee8 (patch) | |
tree | e94e6684371aa0fa151e68c0a46446d902561dbd | |
parent | 5ca86e045bd5742566c647dcc46b8e546a6a7c06 (diff) |
added juxt, juxt[aposes] fns and retuns a fn that returns vector of their results
-rw-r--r-- | src/clj/clojure/core.clj | 37 |
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 |