API里面这样写

reduce(initial, sym) → obj                              reduce(初始值,符号)
reduce(sym) → obj           reduce(符号)
reduce(initial) { |memo, obj| block } → obj reduce(初始值){ |memo , 对象|  块}
reduce { |memo, obj| block } → obj       reduce{ |memo, 对象| 块}

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里面的介绍相同,如下

inject(initial, sym) → obj 
inject(sym) → obj
inject(initial) { |memo, obj| block } → obj
inject { |memo, obj| block } → obj

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方法的更多相关文章

  1. JavaScript - reduce方法,reduceRight方法 (Array)

    JavaScript - reduce方法 (Array) 解释:reduce() 方法接收一个函数作为累加器(accumulator),数组 中的每个值(从左到右)开始合并,最终为一个值. 语法:a ...

  2. JavaScript数组的reduce方法详解

    数组经常用到的方法有push.join.indexOf.slice等等,但是有一个经常被我们忽略的方法:reduce,这个方法简直强大的不要不要的. 我们先来看看这个方法的官方概述:reduce()  ...

  3. JavaScript中reduce()方法

    原文  http://aotu.io/notes/2016/04/15/2016-04-14-js-reduce/   JavaScript中reduce()方法不完全指南 reduce() 方法接收 ...

  4. 在JavaScript函数式编程里使用Map和Reduce方法

    所有人都谈论道workflows支持ECMAScript6里出现的令人吃惊的新特性,因此我们很容易忘掉ECMAScript5带给我们一些很棒的工具方法来支持在JavaScript里进行函数编程,这些工 ...

  5. MapReduce中一次reduce方法的调用中key的值不断变化分析及源码解析

    摘要:mapreduce中执行reduce(KEYIN key, Iterable<VALUEIN> values, Context context),调用一次reduce方法,迭代val ...

  6. reduce 方法 (Array) (JavaScript)

    对数组中的所有元素调用指定的回调函数.该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供. 语法         array1.reduce(callbackfn[, in ...

  7. js数组中的find(), findIndex(), filter(), forEach(), some(), every(), map(), reduce()方法的详解和应用实例

    1. find()与findIndex() find()方法,用于找出第一个符合条件的数组成员.它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该 ...

  8. javascript之reduce()方法的使用

    以前看到reduce方法,总是看得我头皮发麻,今天无意间又遇到他了,于是学习了下,接触之后,觉得这个方法还挺好用的,在很多地方都可以派上用场,比如,数组中元素求和.数组去重.求数组中的最大值或最小值等 ...

  9. Javascript reduce方法

    reduce方法接收一个函数作为累加器,数组中的每个值(从左至右)开始缩减,最终计算为一个值 注意:reduce()对于空数组是不会执行回调函数 语法: array.reduce(function(t ...

  10. [五]java函数式编程归约reduce概念原理 stream reduce方法详解 reduce三个参数的reduce方法如何使用

    reduce-归约 看下词典翻译: 好的命名是自解释的 reduce的方法取得就是其中归纳的含义 java8 流相关的操作中,我们把它理解 "累加器",之所以加引号是因为他并不仅仅 ...

随机推荐

  1. maven的pom.xml配置

    添加tomcat插件配置: <!-- tomcat plugin --> <plugin> <groupId>org.apache.tomcat.maven< ...

  2. HTML5 ---localStorage

    HTML5中提供了localStorage对象可以将数据长期保存在客户端,直到人为清除. localStorage提供了几个方法: 1.存储:localStorage.setItem(key,valu ...

  3. js中常用的操作

    1.js中常用的数组操作 2.js中常用的字符串操作 3.js中常用的时间日期操作 4.定时器

  4. lvs+keepalived+nginx实现高性能负载均衡集群

    一.为什么要使用负载均衡技术? 1.系统高可用性 2.  系统可扩展性 3.  负载均衡能力 LVS+keepalived能很好的实现以上的要求,LVS提供负载均衡,keepalived提供健康检查, ...

  5. C# 委托如何理解 打个比喻

    初学者可能会给winform窗体注册事件,也听过事件是基于委托实现的 那么,委托是什么,事件又是什么,委托和事件是什么关系. 个人喜欢做一些比喻,把这些东西想象成某一个模型,这样方便记忆,理解,随着对 ...

  6. java日常时间处理

    private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", & ...

  7. Neutron Kilo-Liberty-Mitaka各版本区别

    http://blog.csdn.net/bc_vnetwork/article/details/51848623

  8. ue4 plugin的编译加载

    插件Plugin: 本来应该是指一种纯以接口与外界打交道的程序模块,在同一接口背后可以有多种实现,更换实现完全不影响客户端代码(不用重编). 但是在ue4的世界里,插件似乎不是这个意思,仅仅是一种可以 ...

  9. 调整static变量初始化顺序的一个办法

    // wrap the LaunchDir variable in a function to work around static/global initialization order stati ...

  10. .Net Framework运行机制

    首先谈谈.net framework的组成 主要是有两大部分组成:CLR(公共语言运行库)和FCL(Framework类库) CLR的主要功能:和Java虚拟机一样也是一个运行时环境,是一个可由多种编 ...