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 流相关的操作中,我们把它理解 "累加器",之所以加引号是因为他并不仅仅 ...
随机推荐
- 浅析敏感词过滤算法(C++)
为了提高查找效率,这里将敏感词用树形结构存储,每个节点有一个map成员,其映射关系为一个string对应一个TreeNode. STL::map是按照operator<比较判断元素是否相同,以及 ...
- C# 委托和事件 与 观察者模式(发布-订阅模式)讲解 by天命
使用面向对象的思想 用c#控制台代码模拟猫抓老鼠 我们先来分析一下猫抓老鼠的过程 1.猫叫了 2.所有老鼠听到叫声,知道是哪只猫来了 3.老鼠们逃跑,边逃边喊:"xx猫来了,快跑啊!我是老鼠 ...
- 读《UNIX编程艺术》一感
我记得早在2006年的时候就开始频繁使用awk做文本处理方面的工作,07年的时候周围有人用perl,我还感到很不解,觉得写得很复杂,没有awk one liner 那么方便和神奇.一直在了解awk的具 ...
- jQuery停止动画——stop()方法的使用
很多时候需要停止匹配元素正在进行的动画,比如,当鼠标选入元素时显示菜单,鼠标离开时隐藏下拉菜单,如果鼠标移入移出过快的话就会导致动画效果与鼠标的动作不一致的情况,此时stop()就派上用场了. sto ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- zabbix nagios 类nagios 之 不以性能为前提的开发和监控都是瞎扯淡
从最初的nagios到现在强大的zabbix 3.0,我想说,不以性能为前提的开发和监控都是瞎扯淡? 首先我对这两款监控软件的认识: zabbix,很多企业都在用,给人的感觉是很炫,不过我个人觉得虽然 ...
- python 生成 xml文件 属性的顺序问题
需求很奇葩. 文档示例 <ITEM key="username" eng="User Name" chn="用户名" val=&quo ...
- gulp插件gulp-ruby-sass和livereload插件
gulp-ruby-sass是gulp的一个插件,主要是用来实现sass编译,livereload插件主要是实现文件保存时浏览器自动刷新,避免了手动f5的频繁的操作 准备工作:chrome浏览器安装l ...
- jetty服务器启动方法总结【备用】
1. 使用Java命令启动 java -jar start.jar ctrl + c 关闭 终端窗口一直存在 2. 使用Java命令启动2 java -jar start.jar & 启动成功 ...
- OpenSource.organization-in-github
1. gosquared https://github.com/gosquared 2. slack https://github.com/slackhq 3. The New York Times ...