Clojure:减少与应用 [英] Clojure: reduce vs. apply

查看:12
本文介绍了Clojure:减少与应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解 reduceapply 之间的概念区别:

I understand the conceptual difference between reduce and apply:

(reduce + (list 1 2 3 4 5))
; translates to: (+ (+ (+ (+ 1 2) 3) 4) 5)

(apply + (list 1 2 3 4 5))
; translates to: (+ 1 2 3 4 5)

然而,哪一个是更惯用的 clojure?它以一种方式或另一种方式有很大不同吗?从我的(有限的)性能测试来看,reduce 似乎要快一些.

However, which one is more idiomatic clojure? Does it make much difference one way or the other? From my (limited) performance testing, it seems reduce is a bit faster.

推荐答案

reduceapply 当然只是等价的(就返回的最终结果而言)关联需要在可变参数情况下查看所有参数的函数.当它们在结果方面等价时,我会说 apply 总是完全惯用的,而 reduce 是等价的——并且可能会减少一小部分眼睛——在很多常见的情况下.以下是我相信这一点的理由.

reduce and apply are of course only equivalent (in terms of the ultimate result returned) for associative functions which need to see all their arguments in the variable-arity case. When they are result-wise equivalent, I'd say that apply is always perfectly idiomatic, while reduce is equivalent -- and might shave off a fraction of a blink of an eye -- in a lot of the common cases. What follows is my rationale for believing this.

+ 本身是根据 reduce 实现的,用于可变数量的情况(超过 2 个参数).事实上,对于任何可变数量的关联函数来说,这似乎是一种非常明智的默认"方式:reduce 有可能执行一些优化以加快速度——也许通过类似 internal-reduce,最近在 master 中禁用的 1.2 新奇事物,但希望在未来重新引入——在每个可能从 vararg 情况下受益的函数中复制它是愚蠢的.在这种常见情况下,apply 只会增加一点开销.(请注意,这没什么好担心的.)

+ is itself implemented in terms of reduce for the variable-arity case (more than 2 arguments). Indeed, this seems like an immensely sensible "default" way to go for any variable-arity, associative function: reduce has the potential to perform some optimisations to speed things up -- perhaps through something like internal-reduce, a 1.2 novelty recently disabled in master, but hopefully to be reintroduced in the future -- which it would be silly to replicate in every function which might benefit from them in the vararg case. In such common cases, apply will just add a little overhead. (Note it's nothing to be really worried about.)

另一方面,一个复杂的函数可能会利用一些优化机会,这些优化机会不够通用,无法内置到reduce中;那么 apply 会让你利用这些,而 reduce 实际上可能会减慢你的速度.str 提供了在实践中发生的后一种情况的一个很好的例子:它在内部使用了一个 StringBuilder 并且将从 apply 的使用中受益匪浅而不是reduce.

On the other hand, a complex function might take advantage of some optimisation opportunities which aren't general enough to be built into reduce; then apply would let you take advantage of those while reduce might actually slow you down. A good example of the latter scenario occuring in practice is provided by str: it uses a StringBuilder internally and will benefit significantly from the use of apply rather than reduce.

所以,如果有疑问,我会说使用 apply ;如果您碰巧知道它不会通过 reduce 为您购买任何东西(并且这不太可能很快改变),请随意使用 reduce 来削减那个小如果你愿意的话,这是不必要的开销.

So, I'd say use apply when in doubt; and if you happen to know that it's not buying you anything over reduce (and that this is unlikely to change very soon), feel free to use reduce to shave off that diminutive unnecessary overhead if you feel like it.

这篇关于Clojure:减少与应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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