clojure - 为什么这个部分不起作用

标签 clojure partial

这是我的代码:

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

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

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

它引发了这个异常:

ClassCastException java.lang.String cannot be cast to clojure.lang.IFn .repl/eval12557 (form-init2162333644921704923.clj:1)

最佳答案

请参阅 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"

关于clojure - 为什么这个部分不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32929149/

相关文章:

sql - Clojure:读取数据库的元数据

r - 使用 roll_sum 时的部分和

javascript - 如何在 Ruby on Rails 4 中通过 CoffeeScript 进行渲染

clojure - clojurescript 中的嵌套宏

ruby-on-rails - 在Rails中使用局部的最佳实践

Lucene SpanNearQuery 部分匹配

audio - 服务器日志/Webalizer,206 个音频和视频文件的部分内容——我如何计算下载次数?

clojure - 如何更好地迭代 Clojure 中的状态(monad?)

python - 动态语言中的类型类

clojure - Microbenchmark Clojure 函数