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

后端 未结 8 1843
南旧
南旧 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:33

    What about this one? It selects the relevant return values from juxt. Since this is all lazy, it should only calculate the elements needed.

    user> (defn my-juxt [fns & colls] 
            (map-indexed (fn [i e] (e i))
              (apply map (apply juxt fns) colls)))
    #'user/my-juxt
    user> (my-juxt [- + *] [1 2 3] [1 2 3] [1 2 3])
    (-1 6 27)
    

提交回复
热议问题