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

后端 未结 8 1840
南旧
南旧 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

    I personally think your first version is pretty clear and idiomatic.

    Here's an alternative you might find interesting to consider however:

    (map 
      apply 
      [- + *] 
      (map vector [1 2 3] [1 2 3] [1 2 3]))
    
    => (-1 6 27)
    

    Note the trick of using (map vector ....) to transpose the sequence of arguments into ([1 1 1] [2 2 2] [3 3 3]) so that they can be used in the apply function.

提交回复
热议问题