在 clojure 中,如何应用“和”?到一个列表?

发布于 2025-01-04 03:05:13 字数 193 浏览 3 评论 0原文

在闭包中,我们如何将 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 技术交流群。

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

发布评论

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

评论(4

⊕婉儿 2025-01-11 03:05:13

您也可以这样做:

(every? identity '(true false))

请参阅此帖子了解更多信息。

You can do this instead:

(every? identity '(true false))

See this thread for more information.

我要还你自由 2025-01-11 03:05:13

在 Clojure 中,宏不是一流的东西,它们的组合方式与函数不同,您无法将它们传递给其他函数,也无法应用它们。这是因为它们在任何应用程序完成之前就已经完成。

通常将将宏包装在函数中以应用或将它们传递给函数

(defmacro my-macro [x y z] ...)

(apply #(my-macro %1 %2 %3) [1 2 3])

(map #(my-macro) [1 2 3] [:a :b :c] [a b c])

匿名函数读取器宏#(使这变得如此简单,以至于宏不是头等舱确实不是一个不便。只要尝试记住宏的第一条规则即可。俱乐部

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 functions

(defmacro my-macro [x y z] ...)

(apply #(my-macro %1 %2 %3) [1 2 3])

(map #(my-macro) [1 2 3] [:a :b :c] [a b c])

the 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

ˇ宁静的妩媚 2025-01-11 03:05:13

您还可以这样做:
(apply = true '(true false))

You can also do this:
(apply = true '(true false))

寻找一个思念的角度 2025-01-11 03:05:13
user=> (eval `(and ~@(list true false)))

false

user=> (eval `(and ~@ (list true true 1)))

1

我使用语法引用,然后用大锤子删除集合周围的括号,因为我引用了它,所以必须使用 eval。如果您正在创建宏并且传入的函数是“and”,则这非常有用。

所以 (list 1 2 3) 大锤锤打变成 1 2 3

这是一种方法,非常糟糕,但它有效,所以我得到了它,这对我来说很好。
所以我的 clojure 不是很好,但我希望这个宏示例能够澄清问题。

(defmacro split-my-args-then-apply-fn
  "This is one terrible macro designed to show the use of 'and'."
  [fun coll & args]
  `(~fun ~@((apply juxt args) coll)))

用法(从不):

(split-my-args-then-apply-fn 
    and 
    {:something :true :something-else false} 
    :something :something-else)
user=> (eval `(and ~@(list true false)))

false

user=> (eval `(and ~@ (list true true 1)))

1

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 becomes 1 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.

(defmacro split-my-args-then-apply-fn
  "This is one terrible macro designed to show the use of 'and'."
  [fun coll & args]
  `(~fun ~@((apply juxt args) coll)))

Usage (Never):

(split-my-args-then-apply-fn 
    and 
    {:something :true :something-else false} 
    :something :something-else)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文