减少操作 - Reduce operation

- 此内容更新于:2016-02-01
主题:

这是我的一些演讲幻灯片:我不明白发生了什么在这个操作。我不懂为什么打印出来。我不明白发生了什么,行动,但我认为我发现它只是需要最长的单词并返回它。

原文:

This is in some lecture slides of mine:

words = %w{cat sheep bear}
words.reduce do |memo, word| memo.length > word.length ? memo : word end
#=> "sheep"

I don't understand what is going on in this reduce operation. I do not understand why "sheep" is printed.

I didn't understand what was going on in the memo, word operation, but I think that I figured out that it simply takes the longest word and returns it.

网友:

(原文:meta.stackoverflow.com/questions/253894/…)

网友:你的问题是什么?

(原文:What is your question?)

网友:啊我明白了,我会编辑它更具体地说,对不起

(原文:ahhh i see, I'll edit it to be more specific, sorry)

解决方案:
阅读的文档,特别是:如果你指定一个街区,然后为每个元素在enum块传递一个蓄电池值(备忘录)和元素……结果变成了备忘录的新值。在迭代结束时,备忘录的最终价值是方法的返回值。小清晰,说你的块可以写成:块减少接受两个参数:减少的当前值(也叫做备忘录,或蓄电池),和正在处理的当前元素。块返回的值是它使用作为下一次迭代的备忘录。减少在英语,你在这里看着数组中的每个单词,每个迭代后它看到的最长的单词。最终的结果是,这里使用的表达式是返回数组中的最长单词,这是。如果你是通过执行一个迭代步骤,你会看到以下值被传递到块:迭代0,一个初始值没有指定的备忘录,因此减少使用数组中的第一个值(“猫”)作为默认备忘录“猫”仍是我们最长备忘录迭代1=“猫”,因为“羊”是比“猫”,“羊”成为我们新的迭代2的备忘录,“羊”是比“熊”,所以它仍然是我们的备忘录完成我们遍历整个数组,以及由此产生的价值减少调用的值是我们当前的备忘录中,“羊”减少一些语言也被称为折叠。术语“折叠”直观意义当你滚动或折叠数组中的每个值到某种聚合(减少)最终值。
原文:

Read the documentation for Enumerable#reduce, specifically:

If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element...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.

For a little added clarity, your block can be rewritten as:

words.reduce do |longest, word|
  if longest.length > word.length
    longest
  else
    word
  end
end

The block to reduce takes two parameters: the current value of the reduction (also called the memo, or accumulator), and the current element being processed. The value that the block returns is what it uses as the memo for the next iteration.

In English, your reduction here is looking at each word in the array and keeping the longest word that it sees after each iteration. The net result is that the reduce expression used here is returning the longest word in the array, which is sheep.

If you were to step through the execution one iteration at a time, you'd see the following values being passed to the block:

  • Iteration 0
    • longest = "cat", word = "cat"
    • An initial value was not specified for the memo, so reduce uses the first value in the array ("cat") as the default memo
    • "cat" remains our memo
  • Iteration 1
    • longest = "cat", word = "sheep"
    • Because "sheep" is longer than "cat", "sheep" becomes our new memo
  • Iteration 2
    • longest = "sheep", word = "bear"
    • "sheep" is longer than "bear", so it remains as our memo
  • Complete
    • We've iterated through the entire array, and the resulting value for the reduce call is the value of our current memo, "sheep"

Reduce is also known as folding in some languages. The term "folding" makes intuitive sense as you're rolling or folding each value in the array into some kind of aggregate (reduced) final value.

网友:谢谢你,我的新Ruby的语法是最绊倒我。

(原文:thank you, I'm new to Ruby so the syntax is what's tripping me up the most.)

网友:Ruby的语法很简洁,有时神奇。你应该确保有强烈的块和它们是如何工作的,然后进入高级数组操作,,等等,因为一切基于块的概念。我会试着精致的在我的答案。

(原文:Ruby's syntax is terse and sometimes magical. You should be sure to have a strong grasp of blocks and how they work before diving into higher-level array operations like reduce, map, etc., since everything builds on the concept of blocks. I'll try to elaborate a bit in my answer.)

解决方案:
而不是解释,我想给你们看的迭代过程。变量==[‘猫’,‘羊’,‘熊’]目前:目前在方法==‘羊’:备忘录==‘羊’减少方法因为它是迭代数组完成结束。减少方法返回的备忘录将希望这没有让你迷惑。我想也许显示单个迭代就好了
原文:

Rather than explain it, I want to show you the iteration process..

words = %w{cat sheep bear}
words.reduce do |memo, word|
  memo.length > word.length ? memo : word
end

The variable words == ['cat', 'sheep', 'bear']

# First iteration uses 'cat' and 'sheep'
'cat'.length > 'sheep'.length ? 'cat' : 'sheep'
# This ternary returns 'sheep' because 'sheep' is longer than 'cat'

Currently: memo within the reduce method == 'sheep'

# Second iteration uses `memo` which == 'sheep'
# Also uses 'bear'
'sheep'.length > 'bear'.length ? 'sheep' : 'bear'
# `sheep is longer than 'bear', so the ternary is true which returns `sheep`

Currently: memo == 'sheep'

Reduce method ends since it is done iterating the array. Reduce method returns the memo which is set to 'sheep'

Hopefully this didn't confuse you. I thought maybe showing the individual iterations would be nice to see

解决方案:
首先,由于你没有指定默认值的减少将被设置为“猫”。现在备忘录是“猫”。其次,你的代码里面减少方法返回布尔值。默认情况下,如果这个表达式是正确的,那么默认值将被重新分配。这就是为什么备忘录“羊”。最后一个,因为没有更多的单词超过“羊”将返回它。
原文:

First, since you have not specified default value for reduce it will be set to 'cat'. So, now memo is 'cat'. Second, you code inside reduce method returns boolean value. By default, if this expression is true - then the default value will be reassigned. That is why memo is 'sheep'. The last one, since there are no more words longer than 'sheep' it will be returned.

网友:是什么意思“减少默认值”?内部的代码块不返回一个布尔值。不清楚你所说的“默认情况下,…默认值将被重新分配。”

(原文:What do you mean by "default value for reduce"? The code inside the block does not return a boolean value. Not clear what you mean by "By default, ... the default value will be reassigned.")