clojure中的部分应用

clojure中的部分应用,clojure,currying,Clojure,Currying,如何在Clojure中进行部分应用 我试过: (dorun (map println ["1" "2" "3" "4"])) 这很有效 (async/send! channel "hello") 也行。但如果我尝试应用部分应用程序 (dorun (map (async/send! channel) ["1" "2" "3" "

如何在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"]))

上面说

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))
    of
    #(map str%)

  • 使用partial,它将按参数提供的顺序替换参数:

    (部分映射str)

  • 当调用参数少于所需数量的函数时,您将得到
    ArityException
    (除非它是一个多arity函数,可以接受不同数量的参数)。

    部分和
    #()
    生成两种不同类型的函数。使用
    #()
    时,您需要明确指定参数插入的位置,例如
    #(异步/发送!通道%)
        (dorun (map #(async/send! channel) ["1" "2" "3" "4"]))