juxt(apose) function


/ Published in: Clojure
Save to your folder(s)

A nice function which creates a list of function that can be applied during a map.


Copy this code and paste it in your HTML
  1. ; creating a list of functions from fns
  2. (defn juxt [& fns]
  3. (fn[ & arg] (map #(apply % args) fns)))
  4.  
  5. ; a simple map
  6. (def test-map {:a "1" :b "2" :c "3" :d "4"})
  7.  
  8. ; applying two functions at once which results with ("1" "3")
  9. ((juxt :a :c) test-map)
  10.  
  11. ; applying both identity & square on the range values,
  12. ; result with ((1 1) (2 4) (3 9) (4 16) (5 25))
  13. ((partial map (juxt identity #(* % %))) (range 1 6))

URL: http://groups.google.com/group/clojure/browse_thread/thread/c960ec89bd0a85ac

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.