Standard version or idiomatic use of (fn [f & args] (apply f args))

后端 未结 8 1916
南旧
南旧 2021-01-17 12:37

Every so often I find myself wanting to apply a collection of functions on several collections of parameters. It\'s easy to do with map and a very simple function.



        
8条回答
  •  孤独总比滥情好
    2021-01-17 13:37

    Another approach which is fairly self explanatory: "for each nth function, apply it to all nth elements of the vectors":

    (defn my-juxt [fun-vec & val-vecs]
      (for [n (range 0 (count fun-vec))]
        (apply (fun-vec n) (map #(nth % n) val-vecs))))
    
    user> (my-juxt [- + *] [1 2 3] [1 2 3] [1 2 3])
    (-1 6 27)
    

提交回复
热议问题