Clojure的功能#(:jerry @%)意味着什么

Law*_*ing 3 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]))))))
Run Code Online (Sandbox Code Playgroud)

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

谷歌搜索"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]))
         )))
Run Code Online (Sandbox Code Playgroud)

Pio*_*dyl 9

#( ...)匿名函数读者宏,其中%表示传递给函数的第一个参数.例如:

#(println %)

相当于:

(fn [x] (println x))

@这是一个读者宏deref:

@some-variable

是相同的:

(deref some-variable)

并用于从其中一个ref类型中取消引用当前值.

因此#(:jerry @%)是一个匿名函数,当应用于ref(例如一个原子)时,它将使用deref其当前值并将其用作参数,以将:jerry 关键字作为具有该值的函数调用.