有序流的减少量是否会按顺序减少? [英] Does reduction on an ordered stream reduce in order?

查看:85
本文介绍了有序流的减少量是否会按顺序减少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的A,B,C的List.

C reduce A reduce B != A reduce B reduce C(但是,A减少(B减少C)是可以的).

C reduce A reduce B != A reduce B reduce C (however, A reduce (B reduce C) is OK).

换句话说,我的归约运算是关联的而不是可交换的.

In other words, my reduction operation is associative but not commutative.

Java是否对有序顺序流(例如列表中的默认顺序流)强制执行总是根据遇到顺序进行还原的操作?也就是说,java是否会重新排序减少量(例如B减少A而不是A减少B)?

Does java enforce on an ordered sequential stream (such as the default one from a list) that reduction will always happen according to the encounter order? That is to say, will java reorder reductions (such that B reduce A instead of A reduce B)?

(希望这很清楚).

edit 进行一些演示,也许有助于澄清

edit to add a little demo and maybe helping to clarify

Stream.of(" cats ", " eat ", " bats ")
  .reduce("", (a, b) -> a + b); // cats eat bats

使用上述方法,输出结果可能是蝙蝠猫吃"​​还是吃蝙蝠猫"?可以保证在规范中的某处吗?

With the above, could the output ever be "bats cats eat" or "eat bats cats"? Is that guaranteed somewhere in the spec?

推荐答案

我之前的答案不正确(感谢@shmosel纠正了我).

My previous answer was incorrect (thanks to @shmosel for correcting me).

现在我声明,根据规范,它尊重元素的顺序.

Now I state, that according to the specification it respects the order of the elements.

证明很简单.

A proof is very simple. The specification claims that a reduction function has to be associative.

但是, 关联性 ,如果不保留顺序,它本身就没有任何意义.根据关联属性的数学定义:

在一个表达式中包含连续两次或多次出现的表达式 相同的关联运算符,运算的顺序 只要操作数的顺序是,执行的操作就没关系 未更改.

Within an expression containing two or more occurrences in a row of the same associative operator, the order in which the operations are performed does not matter as long as the sequence of the operands is not changed.

换句话说,关联属性并不意味着:

In other words, associative property doesn't imply that:

(a + b) + c = (a + c) + b

它仅允许对应用操作的顺序进行任意排列.

It only allows an arbitrary permutation of the order in which operations are applied.

这篇关于有序流的减少量是否会按顺序减少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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