Clojure 中的磅引号(散列引号,#')是否运行解析和符号函数? [英] Is pound-quote (hash-quote, #') in Clojure running the resolve and symbol functions?

查看:17
本文介绍了Clojure 中的磅引号(散列引号,#')是否运行解析和符号函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许你可以帮我在文档中找到这个.我正在使用磅引号能够在执行之前传递未评估的函数名称.例如:

Perhaps you can help me find this in the docs. I'm using pound-quote to be able to pass around unevaluated function names prior to execution. For example:

(#'cons 1 ())
;(1)

(defn funcrunner [func a b]
  (func a b))

(funcrunner cons 'a ())
;(a)

(funcrunner 'cons 'a ())
'()

(funcrunner #'cons 'a ())
;(a)

#'cons
;#'clojure.core/cons

(resolve (symbol 'cons))
;#'clojure.core/cons

我猜这是一个阅读器宏.

My guess is that this is a reader macro.

我的问题是 (a) 英镑引号 (#') 的简写是什么?(b) 你能解释一下它在做什么吗?(c) 你能在文档中找到它吗?(d) 它实际上是解析和符号函数的简写吗?

My question is (a) What is the pound quote (#') shorthand for? (b) Can you explain what it is doing? (c) Can you locate it in the docs? (d) Is it actually shorthand for for resolve and symbol functions?

PS - 对于不在美国的人 - # 也称为散列"或交叉散列".

PS - For those not in the US - # is also known as a 'hash' or a 'cross-hash'.

PPS - 我知道我的例子使得这有点多余.我很想知道这是完全多余的还是有特定的用例.

PPS - I'm aware my example makes the need for this somewhat redundant. I'm interested to know if this is completely redundant or there are specific use cases.

推荐答案

#' 是一个扩展为 (var foo) 的阅读器宏.您在这里所做的不是传递未计算的函数,而是传递包含函数的变量.这样做的原因是因为 vars 是查找其包含的值并调用它的函数:

#' is a reader macro that expands to (var foo). What you're doing here is not passing around unevaluated functions, you're passing around vars which contain functions. The reason this works the way it does is because vars are functions that look up their contained value and call it:

user=> (defn foo [x] (+ x 10))
#'user/foo
user=> (#'foo 10)
20
user=> ((var foo) 10)
20

请注意,当我定义函数时,返回了一个 var.看起来你一直在做什么!:)

Notice that when I defined the function, a var was returned. It looks like what you've been doing! :)

这篇关于Clojure 中的磅引号(散列引号,#')是否运行解析和符号函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆