我正在解决4Clojure的问题.
我有一个Tic-Tac-Toe练习的工作解决方案,但我无法理解Darren的解决方案:
(fn [b]
(some (fn [p] (first (keep #(if (apply = p %) p)
`(~@b ; <- What is that ` and ~@?
~@(apply map list b) ;
~(map get b [0 1 2])
~(map get b [2 1 0])))))
[:x :o]))
;b is a two-dimensional vector
Run Code Online (Sandbox Code Playgroud)
`和〜@是什么意思?
mik*_*era 12
`是语法引用,用于在不对其进行评估的情况下将代码编写为数据.请注意,它足够聪明,可以将表示函数的符号解析为正确的命名空间.
例子:
`(+ 1 2)
=> (clojure.core/+ 1 2) ; a list containing the + function and two arguments
(eval `(+ 1 2))
=> 3 ; the result of evaluating the list
Run Code Online (Sandbox Code Playgroud)
〜@是unquote-splicing运算符,它可以让你扩展一些引用数据/代码中的元素列表.
例子:
(def args [3 4 5 6])
`(+ 1 2 ~@args 7 8)
=> (clojure.core/+ 1 2 3 4 5 6 7 8)
`(+ ~@(range 10))
=> (clojure.core/+ 0 1 2 3 4 5 6 7 8 9)
Run Code Online (Sandbox Code Playgroud)
关于这些和相关操作的更多细节可以作为Clojure读者文档的一部分找到.
| 归档时间: |
|
| 查看次数: |
1418 次 |
| 最近记录: |