clojure - Clojure 函数是什么#( :jerry @%) means

标签 clojure

我对 Clojure 很陌生。我正在 Clojure Koans 的帮助下学习。我找到了以下代码的答案:

(= ["Real Jerry" "Bizarro Jerry"]
       (do
         (dosync
          (ref-set the-world {})
          (alter the-world assoc :jerry "Real Jerry")
          (alter bizarro-world assoc :jerry "Bizarro Jerry")
          (vec (map #(:jerry @%) [the-world bizarro-world]))))))

来自:https://github.com/viebel/clojure-koans/blob/master/src/koans/16_refs.clj#L42

对于 Google 来说,像“Clojure @%”这样的搜索是非常不友好的。所以我从互联网上一无所获。

它如何用于函数“#(:jerry @%)”?

下面的代码是我的答案,但它不起作用。
(= ["Real Jerry" "Bizarro Jerry"]
       (do
         (dosync
          (ref-set the-world {})
          (alter the-world assoc :jerry "Real Jerry")
          (alter bizarro-world assoc :jerry "Bizarro Jerry")
          (vec (map (fn [x] (:jerry x)) [the-world bizarro-world]))
         )))

最佳答案

#( ...)reader macro for anonymous function哪里%表示传递给函数的第一个参数。例如:
#(println %)
相当于:
(fn [x] (println x))@reader macro for deref 再说一遍:
@some-variable
是相同的:
(deref some-variable)
并用于从 ref types 之一取消引用当前值.

因此#(:jerry @%)是一个匿名函数,当应用于引用(例如原子)时,将 deref它的当前值并将其用作调用 :jerry 的参数keyword as a function与值(value)。

关于clojure - Clojure 函数是什么#( :jerry @%) means,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44052252/

相关文章:

java - 创建文件时如何设置OTHERS_WRITE?

Clojure 形式到字符串

clojure - 如何将我的 defstruct 保留并恢复到文件中?

clojure - Clojure 中的最大公约数

clojure - 如何在 Clojure 的 fn 中使用 undefined symbol ?

clojure - 如何遍历从 Clojure 中的生产者函数生成的树?

clojure - 如何编辑edn文件?

emacs - 如何评估 Emacs init.el 文件中的某些内容?

java - 从 Java 调用 clojure (Clojure Interop)

clojure - 如何在 Clojure 中转换为 String[]?