reduce方法
API里面这样写
Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.
由块或符号命名的一个方法或者操作,通过执行一个二进制操作,将所有枚举的元素结合起来
If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element. If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.
如果你指定了一个块,那么集合里的每个元素被传递给一个收集器(memo,这里是一个元素)里存放的值。如果你指定的是一个符号,那么每个元素将会被传递给一个memo(这时是一个方法)。无论用哪种方法,结果都会变成memo里存放的新值。迭代到最后,memo的将值返回给方法。
If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of memo.
如果你未明确初始化memo的值,那么memo将会被赋值为集合内的第一个元素。
通过上面的理解,我们知道memo就是用来存放结果值的一个值或者方法。
# Sum some numbers 累加
(5..10).reduce(:+) #=> 45
#(5..10).reduce(符号)
# Same using a block and inject 累加
(5..10).inject { |sum, n| sum + n } #=> 45
#(5..10).inject{ |memo, 对象| 块 }
# Multiply some numbers 阶乘
(5..10).reduce(1, :*) #=> 151200
#(5..10).redeuce(初始值,符号) # Same using a block 阶乘
(5..10).inject(1) { |product, n| product * n } #=> 151200
#(5..10).inject(初始值){ |memo , 对象| 块}
# find the longest word
longest = %w{ cat sheep bear }.inject do |memo, word|
memo.length > word.length ? memo : word
end
longest #=> "sheep"
#这个也是利用块,我们可以把代码加上{}
longest = %w{ cat sheep bear }.inject {
|memo, word| memo.length > word.length ? memo : word
}
#现在很好理解了,其实是一个道理,可以这么用就是,理解了这个用法,对写程序的能力提高很大的帮助,%w的意思是将{ cat sheep bear }变为["cat", "sheep", "bear"]。
看到例子中用了inject方法,我们再来看看inject方法是怎么介绍的
去查了下,API里面的介绍相同,如下
Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.
If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element. If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.
If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of memo.
reduce方法的更多相关文章
- JavaScript - reduce方法,reduceRight方法 (Array)
JavaScript - reduce方法 (Array) 解释:reduce() 方法接收一个函数作为累加器(accumulator),数组 中的每个值(从左到右)开始合并,最终为一个值. 语法:a ...
- JavaScript数组的reduce方法详解
数组经常用到的方法有push.join.indexOf.slice等等,但是有一个经常被我们忽略的方法:reduce,这个方法简直强大的不要不要的. 我们先来看看这个方法的官方概述:reduce() ...
- JavaScript中reduce()方法
原文 http://aotu.io/notes/2016/04/15/2016-04-14-js-reduce/ JavaScript中reduce()方法不完全指南 reduce() 方法接收 ...
- 在JavaScript函数式编程里使用Map和Reduce方法
所有人都谈论道workflows支持ECMAScript6里出现的令人吃惊的新特性,因此我们很容易忘掉ECMAScript5带给我们一些很棒的工具方法来支持在JavaScript里进行函数编程,这些工 ...
- MapReduce中一次reduce方法的调用中key的值不断变化分析及源码解析
摘要:mapreduce中执行reduce(KEYIN key, Iterable<VALUEIN> values, Context context),调用一次reduce方法,迭代val ...
- reduce 方法 (Array) (JavaScript)
对数组中的所有元素调用指定的回调函数.该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供. 语法 array1.reduce(callbackfn[, in ...
- js数组中的find(), findIndex(), filter(), forEach(), some(), every(), map(), reduce()方法的详解和应用实例
1. find()与findIndex() find()方法,用于找出第一个符合条件的数组成员.它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该 ...
- javascript之reduce()方法的使用
以前看到reduce方法,总是看得我头皮发麻,今天无意间又遇到他了,于是学习了下,接触之后,觉得这个方法还挺好用的,在很多地方都可以派上用场,比如,数组中元素求和.数组去重.求数组中的最大值或最小值等 ...
- Javascript reduce方法
reduce方法接收一个函数作为累加器,数组中的每个值(从左至右)开始缩减,最终计算为一个值 注意:reduce()对于空数组是不会执行回调函数 语法: array.reduce(function(t ...
- [五]java函数式编程归约reduce概念原理 stream reduce方法详解 reduce三个参数的reduce方法如何使用
reduce-归约 看下词典翻译: 好的命名是自解释的 reduce的方法取得就是其中归纳的含义 java8 流相关的操作中,我们把它理解 "累加器",之所以加引号是因为他并不仅仅 ...
随机推荐
- Debug模式,不能进入打断点的类,反而进入代理类里
有史以来,第一次,遇到这个问题, 设置好断点,Debug模式开启项目,,没有进入原来打好的断点类,反而,进入的是和断点类相同名字(但是图标不同)的一个类里, 不能真正的调试,调试变得很麻烦, 解决方案 ...
- Linux下错误的捕获:全局变量errno和strerror()
经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因.这个时候使用 ...
- java 缓冲
缓存主要可分为二大类: 一.通过文件缓存,顾名思义文件缓存是指把数据存储在磁盘上,不管你是以XML格式,序列化文件DAT格式还是其它文件格式: 二.内存缓存,也就是实现一个类中静态Map,对这个Map ...
- eclipse的安装与配置
eclipse的英文名是日蚀,一直很喜欢这个名字. 1.安装很简单,直接下载eclipse包,免安装的.解压后找到其执行文件,如图所示.
- linq 实现group by 不使用group关键字 等同lambad表达式中的group join 查询一对多关系
return from orderInfo in orderEntity.x_s_orderInfo join oState in orderEntity.x_s_oStatuInfo on orde ...
- Android 设置代理(验证用户名和密码)
这几天在研究在Android中,解析网页,但是公司内容,链接外网需要代理,并需要验证用户名和密码,十分头疼,网上查了下,没有头绪,最后总算在一个外国博客中看到类似的,记录下 URL url = new ...
- JavaScript基础知识整理(2)
15.处理图像 注意:(1)在写js文件时,尽量将函数的声明往后写,将函数调用写在前面,这样能够使代码结构很清晰. (2)一个网页中翻转器一般超过3个,所以使用for循环减少重复使用翻转器代码的次数. ...
- Module 'fileinfo' already loaded in Unknown on line 0
出现的原因是:需要加载的扩展已经以而二进制文件的形式写入了php中,但是,在php.ini中却再一次动态加载 参考出处
- java实现调用ORACLE中的游标和包
今天把oracle中的包和游标学习了下,不废话,网上的的有些代码是错误的,抄来抄去,就自己实践了下,做个记录.直接上图,上代码 通过plsql创建自己的的包,包分为包头和包体. 1.包头如下: CRE ...
- [转]学术型 github 畅想
转自 http://wulfric.me/2013/09/github-and-academy/ 以 github 的精神提供学术服务,也许是一个不错的方向. 什么是 github? Github 是 ...