scala fold系列函数及 sparkRDD fold 操作解析
来源:互联网 发布:数据库中column 编辑:程序博客网 时间:2024/05/17 22:41
scala 的fold系列 函数用起来比较方便,这里对比总结一下。
fold
fold 的定义:
deffold[A1 >: A](z: A1)(op: (A1, A1) ⇒ A1): A1
fold 函数的操作顺序是不确定的,而且 A1 是 A 的超类,这是一个比较有用的特性在并发计算的时候,因为,对于fold的中间计算结果,是允许超类之间合并。
foldLeft
foldLeft 的定义:
deffoldLeft[B](z: B)(f: (B, A) ⇒ B): B
Applies a binary operator to a start value and all elements of this sequence, going left to right.
Note: will not terminate for infinite-sized collections.
foldLeft 有一个特殊的符号表示:/:
def /:[B](z: B)(op: (B, A) => B): B = foldLeft(z)(op)
foldRight
foldRight 是先将数据reverse,然后调用foldLeft。对应的,foldRight也有一个特殊的符号表示::\
def foldRight[B](z: B)(op: (A, B) => B): B = reversed.foldLeft(z)((x, y) => op(y, x))
def :\[B](z: B)(op: (A, B) => B): B = foldRight(z)(op)
fold、foldLeft 函数 与reduce 有什么区别?
reduce 可是用于并行化操作,foldLeft 则不可以,这个对于分布式计算框架非常重要,这也是为什么spark等要保留reduce操作。
但是,在spark中,没有fold函数,那是因为:
fold 需要计算数据是有序的,reduce没有这个要求。fold中的操作,(x op y != y op x),reduce满足交换律。
这个问题在stackoverflow上有一个比较好的解释:
http://stackoverflow.com/questions/25158780/difference-between-reduce-and-foldleft-fold-in-functional-programming-particula?lq=1
- scala fold系列函数及 sparkRDD fold 操作解析
- Fold
- fold
- scala List reduce和fold对比分析
- vim fold
- Fold expressions
- 关于fold
- Rust :fold
- 函数式编程里面的fold
- Scala:fold,foldLeft和foldRight区别与联系
- Scala:fold,foldLeft和foldRight区别与联系
- Scala:fold,foldLeft和foldRight区别与联系
- SICP 习题2.38 fold-left fold-right
- RDD行动Action操作(3)–aggregate、fold、lookup
- 3.4 Spark RDD Action操作3-聚合-aggregate、fold、reduce
- 10-fold cross-validation
- VIM fold 折叠
- linux命令--fold
- 开发板AN4.3平台的四色键无效的解法
- 黑马程序员-iOS基础-Objective-C基础(二)面向对象思想及类
- 关于正向代理,反向代理的基本认识
- 今天有一个人退款
- Redis---Linux环境安装
- scala fold系列函数及 sparkRDD fold 操作解析
- Qt64位编译时,public: void __cdecl QEventDispatcherWin32Private::doWsaAsyncSelect(int).
- 解决IE系列浏览器上传页面接收问题
- 闹铃
- <%=a%>
- QT210烧写UBOOT到SD卡原理以及UBOOT启动
- Hdu 1520 Anniversary party (树形dp入门,类似最大独立集)
- Cognos函数(二十四) - descendants
- 【iOS开发-95】JSON反序列化、XML数据解析以及主线程中的UI更新等小细节
Folds the elements of this traversable or iterator using the specified associative binary operator.
The order in which operations are performed on elements is unspecified and may be nondeterministic.
a type parameter for the binary operator, a supertype of
A
.a neutral element for the fold operation; may be added to the result an arbitrary number of times, and must not change the result (e.g.,
Nil
for list concatenation, 0 for addition, or 1 for multiplication.)a binary operator that must be associative
the result of applying fold operator
op
between all the elements andz