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 流相关的操作中,我们把它理解 "累加器",之所以加引号是因为他并不仅仅 ...
随机推荐
- centos如何卸载软件
需要看你的软件包格式: 如果你带有yum,可以直接yum remove xxx如果是rpm包,rpm -e xxxtar包的话需要你直接删除该文件或者make uninstall xxx常见的就这三种
- NetworkComms 文件上传下载和客户端自动升级(非开源)
演示程序下载地址:http://pan.baidu.com/s/1geVfmcr 淘宝地址:https://shop183793329.taobao.com 联系QQ号:3201175853 许可:购 ...
- http之100-continue(转)
1.http 100-continue用于客户端在发送POST数据给服务器前,征询服务器情况,看服务器是否处理POST的数据,如果不处理,客户端则不上传POST数据,如果处理,则POST上传数据.在现 ...
- linux RPM、YUM
Linux 界的两大主流: RPM 与 DPKG distribution 代表 软件管理机制 使用指令 在线升级机制(指令) Red Hat/Fedora RPM rpm, rpmbuild YUM ...
- iava多线程详解(2)-成员变量与局部变量访问
有两段代码 1.线程访问成员变量 public class FirstThreadTest { public static void main(String[] args) { FirstThread ...
- 3.使用git提交项目到开源中国(gitosc)
1.提交地址 使用的是开源中国git仓库 git.oschina.net 在windos环境下使用msysgit. 2.初始化化 username.email初始化 git config --glob ...
- 23种oop设计模式定义
创建型模式 单例模式:确保一个类只有一个实例,而且自行实例化并向整个系统提供这个实现. 工厂模式:定义一个用于创建对象的接口,让子类决定将哪一个类实例化.工厂方法使一个类的实例化延迟到子类. 抽象 ...
- 使用echarts
<html> <head> <script type="text/javascript" src='echarts.min.js'></s ...
- AngularJs自定义指令详解(10) - 执行次序
代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8 ...
- dos2unix unix2dos
实现windows和linux之间的文件自动转换,消除^M.