在 clojure 中的部分应用
partial application in clojure
如何在 Clojure 中进行部分应用?
我试过:
(dorun (map println ["1" "2" "3" "4"]))
有效。
(async/send! channel "hello")
也可以。但是如果我尝试申请部分申请
(dorun (map (async/send! channel) ["1" "2" "3" "4"]))
或
(dorun (map #(async/send! channel) ["1" "2" "3" "4"]))
或
(apply (partial map (async/send! channel) ["1" "2" "3" "4"]))
它说
clojure.lang.ArityException: Wrong number of args (1) passed to: immutant.web.async/send!
我做错了什么?
没关系,这似乎有效:
(dorun (map (partial async/send! channel) ["1" "2" "3" "4"]))
有点困惑为什么这不起作用
(dorun (map #(async/send! channel) ["1" "2" "3" "4"]))
Clojure 中的柯里化不同于 ML、F# 或 Haskell 等语言。
在 Clojure 中有两种方法可以进行部分应用:
制作闭包,您可以在其中指定参数的确切顺序:
(fn [coll] (map str coll))
个 #(map str %)
使用部分,它将按参数提供的顺序替换参数:
(partial map str)
当您使用少于所需参数的函数调用函数时,您将得到 ArityException
(除非它是一个 multi-arity 函数,它可以接受不同数量的参数)。
如何在 Clojure 中进行部分应用?
我试过:
(dorun (map println ["1" "2" "3" "4"]))
有效。
(async/send! channel "hello")
也可以。但是如果我尝试申请部分申请
(dorun (map (async/send! channel) ["1" "2" "3" "4"]))
或
(dorun (map #(async/send! channel) ["1" "2" "3" "4"]))
或
(apply (partial map (async/send! channel) ["1" "2" "3" "4"]))
它说
clojure.lang.ArityException: Wrong number of args (1) passed to: immutant.web.async/send!
我做错了什么?
没关系,这似乎有效:
(dorun (map (partial async/send! channel) ["1" "2" "3" "4"]))
有点困惑为什么这不起作用
(dorun (map #(async/send! channel) ["1" "2" "3" "4"]))
Clojure 中的柯里化不同于 ML、F# 或 Haskell 等语言。
在 Clojure 中有两种方法可以进行部分应用:
制作闭包,您可以在其中指定参数的确切顺序:
(fn [coll] (map str coll))
个#(map str %)
使用部分,它将按参数提供的顺序替换参数:
(partial map str)
当您使用少于所需参数的函数调用函数时,您将得到 ArityException
(除非它是一个 multi-arity 函数,它可以接受不同数量的参数)。