Difference between fold and reduce revisted

三世轮回 提交于 2019-12-06 13:40:40

As mentioned in the comments, the term reduce means different thing when used in the context of MapReduce and when used in the context of functional programming.

  • In MapReduce, the system groups the results of the map function by a given key and then calls the reduce operation to aggregate values for each group (so reduce is called once for each group). You can see it as a function (K, [V]) -> R taking the group key K together with all the values belonging to the group [V] and producing some result.

  • In functional programming, reduce is a function that aggregates elements of some collection when you give it an operation that can combine two elements. In other words, you define a function (V, V) -> V and the reduce function uses it to aggregate a collection [V] into a single value V.

When you want to add numbers [1,2,3,4] using + as the function, the reduce function can do it in a number of ways:

  1. It can run from the start and calculate ((1+2)+3)+4)
  2. It can also calculate a = 1+2 and b = 3+4 in parallel and then add a+b!

The foldLeft operation is, by definition always proceeding from the left and so it always uses the evaluation strategy of (1). In fact, it also takes an initial value, so it evaluates something more like (((0+1)+2)+3)+4). This makes foldLeft useful for operations where the order matters, but it also means that it cannot be implemented for unordered collections (because you do not know what "left" is).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!