Posted By

narkisr on 12/30/09


Tagged

apply juxtapose funtion


Versions (?)

juxt(apose) function


 / Published in: Clojure
 

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

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

  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))

Report this snippet  

You need to login to post a comment.