我可以使用带有' vec&#x27的引号的表单返回未评估的列表作为向量。在Clojure中,如何使用函数参考?

发布于 2025-01-31 15:34:20 字数 401 浏览 5 评论 0原文

这是4clojure替补中反复试验的产物,这可能是特殊的,尤其是

预期的行为:

(vec (repeat 10 0))
=> [0 0 0 0 0 0 0 0 0 0]

使用引用的形式,我得到了这个结果:

(vec '(repeat 10 0))
=> [repeat 10 0]

这令人困惑。

我可以通过将其转换回列表并使用“评估”来评估该序列,但是在返回值中,“重复”到底是什么?我可以使用(首先[重复10 0])来访问序列的其他成员,并调用它?在4clojure repl,((第一(vec'(重复10 0))))10 0)返回0

This was the product of trial and error in the 4clojure REPL, and this may be a peculiarity of that in particular,

Intended behaviour:

(vec (repeat 10 0))
=> [0 0 0 0 0 0 0 0 0 0]

Using the quoted form I get this result:

(vec '(repeat 10 0))
=> [repeat 10 0]

This is puzzling.

I can evaluate this sequence by converting it back to a list and using 'eval', but what exactly is 'repeat' here in the return value? Can I access it apart from the other members of the sequence, using (first [repeat 10 0]) for example, and call it? In the 4Clojure REPL, ((first (vec '(repeat 10 0))) 10 0) returns 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

知足的幸福 2025-02-07 15:34:20

表达式'(重复10 0)返回引用列表( 报价停止评估)。 重复不会是函数,而是符号:

(map type '(repeat 10 0))
=> (clojure.lang.Symbol java.lang.Long java.lang.Long)

您可以使用第一个并仅获取此符号,但是如果要调用它,则必须将其转换为功能 resolve

((resolve (first '(repeat 10 0))) 5 0)
=> (0 0 0 0 0)

如果您将使用vector在创建向量之前,将评估每个元素:

[repeat 10 0]
=> [#object[clojure.core$repeat 0x2a4d87a "clojure.core$repeat@2a4d87a"] 10 0]

(map type [repeat 10 0])
=> (clojure.core$repeat java.lang.Long java.lang.Long)

在这种情况下,您甚至不需要resolve

((first [repeat 10 0]) 5 0)
=> (0 0 0 0 0)

为什么((first(vec'(vec'(重复10 0)))) )10 0)返回0?因为此表达式('重复10 0)不调用函数 重复

此表达式在某物上调用符号。您可以做到这一点,符号实现ifn,因此它们为“可callable” 。 可以使用 ifn?

(ifn? 'repeat)
=> true

您 功能不仅是“可呼叫” ,还包括符号,关键字,向量,地图或集合。

当符号被调用时,函数实际上称为 get 。因此,的含义('重复10 0)是:搜索'对象中的重复 10,如果您找不到它,返回0。在更多示例中看​​到这一点:

('repeat {'foo 1 'bar 2})
=> nil

('repeat {'foo 1 'bar 2} 0)
=> 0

('repeat {'foo 1 'bar 2 'repeat 3})
=> 3

('repeat #{'foo 'bar})
=> nil

('repeat #{'foo 'bar} 0)
=> 0

('repeat #{'foo 'bar 'repeat})
=> repeat

Expression '(repeat 10 0) returns quoted list (quote stops evaluation). repeat won't be function, but symbol:

(map type '(repeat 10 0))
=> (clojure.lang.Symbol java.lang.Long java.lang.Long)

You can use first and get only this symbol, but if you want to call it, you have to convert it into function with resolve:

((resolve (first '(repeat 10 0))) 5 0)
=> (0 0 0 0 0)

If you will use vector, each element will be evaluated before vector is created:

[repeat 10 0]
=> [#object[clojure.core$repeat 0x2a4d87a "clojure.core$repeat@2a4d87a"] 10 0]

(map type [repeat 10 0])
=> (clojure.core$repeat java.lang.Long java.lang.Long)

And in this case, you don't even need resolve:

((first [repeat 10 0]) 5 0)
=> (0 0 0 0 0)

And why does ((first (vec '(repeat 10 0))) 10 0) returns 0? Because this expression ('repeat 10 0) doesn't call function repeat.

This expression calls symbol on something. You can do that, symbols implement Ifn, so they are "callable". You can check that with ifn?:

(ifn? 'repeat)
=> true

As you can see, not only functions are "callable", but also symbols, keywords, vectors, maps or sets.

When symbol is called on something, function actually called is get. So, meaning of ('repeat 10 0) is this: search for 'repeat in object 10 and if you don't find it, return 0. See this in more examples:

('repeat {'foo 1 'bar 2})
=> nil

('repeat {'foo 1 'bar 2} 0)
=> 0

('repeat {'foo 1 'bar 2 'repeat 3})
=> 3

('repeat #{'foo 'bar})
=> nil

('repeat #{'foo 'bar} 0)
=> 0

('repeat #{'foo 'bar 'repeat})
=> repeat
錯遇了你 2025-02-07 15:34:20

引用列表(重复10 0) 符号 重复将返回而不是clojure.core.core.core $ repot

当您调用符号作为函数时,符号的实现说明发生了什么在您的情况下,您正在调用符号重复并传递两个参数10 10 和0 。第二个参数0notfound在上方链接的实现中看到的值。

如果您在符号重复上调用resolve,则可以使用返回值(##'clojure.core.core/repote)来调用。

When you quoted the list (repeat 10 0) the symbol repeat is returned instead of clojure.core$repeat.

When you invoke a symbol as a function, this implementation of symbol explains what happens In your case you are invoking the symbol repeat and passing it two arguments 10 and 0. The second argument 0 is the notFound value seen in the implementation linked just above.

If you call resolve on the symbol repeat, you could use the return value (#'clojure.core/repeat) to invoke.

吹梦到西洲 2025-02-07 15:34:20

如果您引用某些内容(使用'),则引用其中的所有内容。这
是从解决功能等选择中选择。

因此,您可以从'(重复10 0)一个列表中,其中包含符号重复>重复
和编号10和0。

要在此处清楚:重复您看到不是 函数。
这只是一个象征。如果您需要该功能,则必须
Resolve IT。如果您想仅掌握该功能,您将
不引用列表。而且总是有eval的选项。

user=> (def x '(repeat 10 0))
#'user/x
user=> (-> x first type)
clojure.lang.Symbol
user=> (type repeat)
clojure.core$repeat

If you quote something (with '), you quote everything inside it. This
is opting out of resolving functions etc.

So you get from '(repeat 10 0) a list, that contains a symbol repeat
and the number 10 and 0.

To be clear here: the repeat you see there is not the function.
It's just a symbol. If you want the function from it, you would have to
resolve it. If you want to get hold just of the function, you would
not quote the list. And there is always the option to eval.

user=> (def x '(repeat 10 0))
#'user/x
user=> (-> x first type)
clojure.lang.Symbol
user=> (type repeat)
clojure.core$repeat
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文