为什么这部分不起作用

时间:2015-10-04 01:23:07

标签: clojure partial

这是我的代码:

(def partial-join (partial (clojure.string/join ",")))

=>(clojure.string/join "," ["foo" "bar"])
"foo,bar"

=> (partial-join ["foo" "bar"])

它引发了这个例外:

  

ClassCastException java.lang.String无法强制转换为clojure.lang.IFn .repl / eval12557(form-init2162333644921704923.clj:1)

2 个答案:

答案 0 :(得分:2)

请参阅clojure.string/join的文档。

clojure.string/join
([coll] [separator coll])
  Returns a string of all elements in coll, as returned by (seq coll),
   separated by an optional separator.

当只为clojure.string/join提供一个参数时,此函数将其参数视为集合,因此:

user=> (clojure.string/join ",")
","

接下来,请参阅partial的文档。

clojure.core/partial
([f] [f arg1] [f arg1 arg2] [f arg1 arg2 arg3] [f arg1 arg2 arg3 & more])
  Takes a function f and fewer than the normal arguments to f, and
  returns a fn that takes a variable number of additional args. When
  called, the returned function calls f with args + additional args.

当只提供一个参数时,partial返回其参数。

user=> (partial (clojure.string/join ",")) 
","

试试这个:

user=> (def partial-join (partial clojure.string/join ","))
#'user/partial-join
user=> (partial-join ["a" "b"])
"a,b"

答案 1 :(得分:-1)

哇。斯图尔特霍洛威使用这个问题作为如何不在费城Clojure Conj调试Clojure的例子:

https://youtu.be/FihU5JxmnBg

我认为这不是一个糟糕的菜鸟问题(并且接受的答案非常有帮助),但他真的很讨厌它是如何表达/构建的。