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

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

问题描述

也许你可以帮我在文档中找到这个。我使用pound-quote能够在执行之前传递未经评估的函数名称。例如:

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.

推荐答案

#'是一个读取器宏, $ c>(var foo)。你在这里做的是不传递未完成的函数,你传递的vars包含函数。之所以这样工作的原因是因为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中的pound-quote(hash-quote,#')运行解析和符号函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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