这三个方法都可以用来遍历数组或者对象,参数也基本相同,有什么大的区别么?
欢迎选择我的课程,让我们一起见证您的进步~~
函数fmap是A到B的映射,
fmap :: A -> B
freduce是A和B到A的映射,
freduce :: A -> B -> A
feach是A到空的映射,
feach :: A -> Unit
[A]代表A类型元素的集合,那么:
map: fmap -> [A] -> [B]
即从A类型的集合映射到B类型的集合
foreach: feach -> [A] -> [Unit]
即在A类型的集合上对每一个元素做同样的操作
reduce: freduce -> A -> [B] -> A
即用一个初始值,一个reduce函数和一个B类型的集合,通过不断的“折叠”,最后得到结果,形象的讲是: freduce(A0, B1) -> A1 freduce(A1, B2) -> A2 ...
reduce也叫做fold
JavaScript里这些都叫Collections. 翻译过来可能叫集合. 关于具体的语法,可以看这里.(就拿Underscore或者Backbone做例子吧) maphttp://learningcn.com/underscore/#map eachhttp://learningcn.com/underscore/#eac... reducehttp://learningcn.com/underscore/#red... 不过话说过来,这些都是数据结构里的概念. 不光JavaScript里有. 其他语言里也有的. 只是语法不同而已.
JavaScript
Collections
函数fmap是A到B的映射,
freduce是A和B到A的映射,
feach是A到空的映射,
[A]代表A类型元素的集合,那么:
即从A类型的集合映射到B类型的集合
即在A类型的集合上对每一个元素做同样的操作
即用一个初始值,一个reduce函数和一个B类型的集合,通过不断的“折叠”,最后得到结果,形象的讲是:
freduce(A0, B1) -> A1
freduce(A1, B2) -> A2
...
reduce也叫做fold
JavaScript
里这些都叫Collections
.翻译过来可能叫集合.
关于具体的语法,可以看这里.(就拿Underscore或者Backbone做例子吧)
map
http://learningcn.com/underscore/#map
each
http://learningcn.com/underscore/#eac...
reduce
http://learningcn.com/underscore/#red...
不过话说过来,这些都是数据结构里的概念. 不光
JavaScript
里有. 其他语言里也有的. 只是语法不同而已.