• (juxt f)
  • (juxt f g)
  • (juxt f g h)
  • (juxt f g h & fs)
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)]

5 Examples top

  • ;; Extract values from a map.
    
    user=> ((juxt :a :b) {:a 1 :b 2 :c 3 :d 4})
    [1 2]
    
  • ;; "Explode" a value.
    
    user=> ((juxt identity name) :keyword)
    [:keyword "keyword"]
    
    
    ;; eg. to create a map:
    
    user=> (into {} (map (juxt identity name) [:a :b :c :d]))
    {:a "a" :b "b" :c "c" :d "d"}
    
  • ;; Get the first character and length of string
    
    user=> ((juxt first count) "Clojure Rocks")
    [\C 13]
    
  • ;; Create lookup maps via a specific key
    
    user=> (defn index-by [coll key-fn]
             (into {} (map (juxt key-fn identity) coll)))
    #'user/index-by
    user=> (index-by [{:id 1 :name "foo"} 
                      {:id 2 :name "bar"} 
                      {:id 3 :name "baz"}] :id)
    {1 {:name "foo", :id 1}, 2 {:name "bar", :id 2}, 3 {:name "baz", :id 3}}
    
    user=> (index-by [{:id 1 :name "foo"} 
                      {:id 2 :name "bar"} 
                      {:id 3 :name "baz"}] :name)
    {"foo" {:name "foo", :id 1}, "bar" {:name "bar", :id 2}, "baz" {:name "baz", :id 3}}
    
  • ;; sort list of maps by multiple values
    user => (sort-by (juxt :a :b) [{:a 1 :b 3} {:a 1 :b 2} {:a 2 :b 1}]
    [{:a 1 :b 2} {:a 1 :b 3} {:a 2 :b 1}]
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:2296 top

(defn juxt 
  "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)]"
  {:added "1.1"
   :static true}
  ([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) (h 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
         ([] (reduce1 #(conj %1 (%2)) [] fs))
         ([x] (reduce1 #(conj %1 (%2 x)) [] fs))
         ([x y] (reduce1 #(conj %1 (%2 x y)) [] fs))
         ([x y z] (reduce1 #(conj %1 (%2 x y z)) [] fs))
         ([x y z & args] (reduce1 #(conj %1 (apply %2 x y z args)) [] fs))))))
Vars in clojure.core/juxt:
Used in 0 other vars

Comments top

1 comment(s) for juxt.

"reduce1" -> "reduce"

core sources from 33 line to 37 line

Log in to add a comment.