在 clojure 中,如何应用“和”?到一个列表?
在闭包中,我们如何将 and
或任何其他宏应用于列表?
这不起作用:
(apply and '(true false))
因为 apply
无法获取宏的值。
那么,检查列表中所有元素是否均为 true 的最佳方法是什么?
In closure, how can we apply and
or any other macro to a list?
This doesn't work:
(apply and '(true false))
Because apply
can't take value of a macro.
So, what is the best way to check if all the elements of a list are true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您也可以这样做:
请参阅此帖子了解更多信息。
You can do this instead:
See this thread for more information.
在 Clojure 中,宏不是一流的东西,它们的组合方式与函数不同,您无法将它们传递给其他函数,也无法
应用
它们。这是因为它们在任何应用程序完成之前就已经完成。通常将将宏包装在函数中以
应用
或将它们传递给函数,匿名函数读取器宏
#(
使这变得如此简单,以至于宏不是头等舱确实不是一个不便。只要尝试记住宏的第一条规则即可。俱乐部In Clojure macros are not first class things they don't compose quite like functions, and you cant pass them to other functions, and you can't
apply
them. This is because they are finished and done with before any of the applying would be done.It is customary to wrap macros in functions to
apply
or pass them to functionsthe anonymous-function reader macro
#(
makes this so easy that macroes not being first class is really not an inconvenience. Just do try to remember the first rule of macro club您还可以这样做:
(apply = true '(true false))
You can also do this:
(apply = true '(true false))
我使用语法引用,然后用大锤子删除集合周围的括号,因为我引用了它,所以必须使用 eval。如果您正在创建宏并且传入的函数是“and”,则这非常有用。
所以
(list 1 2 3)
大锤锤打变成1 2 3
。这是一种方法,非常糟糕,但它有效,所以我得到了它,这对我来说很好。
所以我的 clojure 不是很好,但我希望这个宏示例能够澄清问题。
用法(从不):
I am syntax-quoting and then applying a big sledge hammer to remove the brackets around the collection and since I quoted it, have to use eval. This is useful if you are creating a macro and the function passed in is 'and'.
so
(list 1 2 3)
sledge hammered becomes1 2 3
.This is one way, extremely terrible, but it works, so I got that going for me which is nice.
So my clojure isn't great but I hope this macro example clears things up.
Usage (Never):