为什么在减法中使用不一致的结果会减少? [英] Why inconsistent results using subtraction in reduce?
本文介绍了为什么在减法中使用不一致的结果会减少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下内容:
val rdd = List(1,2,3)
我假设rdd.reduce((x,y) => (x - y))
将返回-4
(即(1-2)-3=-4
),但是它返回了2
.
I assumed that rdd.reduce((x,y) => (x - y))
would return -4
(i.e. (1-2)-3=-4
), but it returned 2
.
为什么?
推荐答案
From the RDD source code (and docs):
/**
* Reduces the elements of this RDD using the specified commutative and
* associative binary operator.
*/
def reduce(f: (T, T) => T): T
reduce
是一个单调约简,因此假定该函数为可交换和 associative ,这意味着不能保证将其应用于元素的顺序.
reduce
is a monoidal reduction, thus it assumes the function is commutative and associative, meaning that the order of applying it to the elements is not guaranteed.
很显然,您的函数(x,y)=>(x-y)
不是可交换的也不是关联的.
Obviously, your function (x,y)=>(x-y)
isn't commutative nor associative.
在您的情况下,reduce可能是这样应用的:
In your case, the reduce might have been applied this way:
3 - (2 - 1) = 2
或
1 - (2 - 3) = 2
这篇关于为什么在减法中使用不一致的结果会减少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文