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

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

    (map #(%1 %2 %3 %4) [- + *][1 2 3][1 2 3][1 2 3])
    
    (-1 6 27)
    

    The problem is that if you want to allow a variable number of arguments, the & syntax puts the values in a vector, necessitating the use of apply. Your solution looks fine to me but as Brandon H points out, you can shorten it to #(apply %1 %&).

    As the other answerers have noted, it has nothing to do with funcall which I think is used in other Lisps to avoid ambiguity between symbols and functions (note that I called the function as (%1 ...) here, not (funcall %1 ...).

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题