Ruby 减少操作
这是我的一些演讲幻灯片:Ruby 减少操作,ruby,reduce,Ruby,Reduce,这是我的一些演讲幻灯片: words = %w{cat sheep bear} words.reduce do |memo, word| memo.length > word.length ? memo : word end #=> "sheep" 我不明白在这个reduce操作中发生了什么。我不明白为什么要打印“sheep” 我不明白备忘录,word操作中发生了什么,但我想我发现它只需要最长的单词并返回它 首先,由于您尚未为reduce指定默认值,它将被设置为“cat”。所以,现
words = %w{cat sheep bear}
words.reduce do |memo, word| memo.length > word.length ? memo : word end
#=> "sheep"
我不明白在这个reduce
操作中发生了什么。我不明白为什么要打印“sheep”
我不明白
备忘录
,word
操作中发生了什么,但我想我发现它只需要最长的单词并返回它 首先,由于您尚未为reduce指定默认值,它将被设置为“cat”。所以,现在备忘录是“猫”。
其次,在reduce方法中编写代码以返回布尔值。默认情况下,如果此表达式为true,则将重新指定默认值。这就是为什么备忘录是“羊”。
最后一个,因为没有比“sheep”长的字了,它将被返回。首先,因为您没有为reduce指定默认值,它将被设置为“cat”。所以,现在备忘录是“猫”。 其次,在reduce方法中编写代码以返回布尔值。默认情况下,如果此表达式为true,则将重新指定默认值。这就是为什么备忘录是“羊”。
最后一个,因为没有比“sheep”更长的单词,所以将返回它。请阅读文档,特别是: 如果指定块,则对于枚举中的每个元素,该块都是 传递累加器值(memo)和元素…结果成为新的 备忘录的价值。在迭代结束时,memo的最终值 是方法的返回值 为了增加一点清晰度,您的块可以重写为:
words.reduce do |longest, word|
if longest.length > word.length
longest
else
word
end
end
要减少的块采用两个参数:减少的当前值(也称为备忘录或累加器)和正在处理的当前元素。块返回的值是它用作下一次迭代的备忘的值
在英语中,这里的归约是查看数组中的每个单词,并保留每次迭代后看到的最长单词。最终结果是这里使用的reduce
表达式返回数组中最长的单词,即sheep
如果一次执行一个迭代,您将看到以下值被传递到块:
- 迭代0
,longest=“cat”
word=“cat”
- 没有为备忘录指定初始值,因此
使用数组中的第一个值(“cat”)作为默认备忘录reduce
- “猫”仍然是我们的备忘录
- 迭代1
,longest=“cat”
word=“sheep”
- 因为“羊”比“猫”长,“羊”成了我们的新备忘录
- 迭代2
,longest=“sheep”
word=“bear”
- “羊”比“熊”长,所以它仍然是我们的备忘录
- 完整的
- 我们已经遍历了整个数组,
调用的结果值是当前备忘录“sheep”的值reduce
- 我们已经遍历了整个数组,
在某些语言中,Reduce也称为。术语“折叠”在将数组中的每个值滚动或折叠为某种聚合(减少的)最终值时具有直观的意义。请阅读文档,特别是: 如果指定块,则对于枚举中的每个元素,该块都是 传递累加器值(memo)和元素…结果成为新的 备忘录的价值。在迭代结束时,memo的最终值 是方法的返回值 为了增加一点清晰度,您的块可以重写为:
words.reduce do |longest, word|
if longest.length > word.length
longest
else
word
end
end
要减少的块采用两个参数:减少的当前值(也称为备忘录或累加器)和正在处理的当前元素。块返回的值是它用作下一次迭代的备忘的值
在英语中,这里的归约是查看数组中的每个单词,并保留每次迭代后看到的最长单词。最终结果是这里使用的reduce
表达式返回数组中最长的单词,即sheep
如果一次执行一个迭代,您将看到以下值被传递到块:
- 迭代0
,longest=“cat”
word=“cat”
- 没有为备忘录指定初始值,因此
使用数组中的第一个值(“cat”)作为默认备忘录reduce
- “猫”仍然是我们的备忘录
- 迭代1
,longest=“cat”
word=“sheep”
- 因为“羊”比“猫”长,“羊”成了我们的新备忘录
- 迭代2
,longest=“sheep”
word=“bear”
- “羊”比“熊”长,所以它仍然是我们的备忘录
- 完整的
- 我们已经遍历了整个数组,
调用的结果值是当前备忘录“sheep”的值reduce
- 我们已经遍历了整个数组,
在某些语言中,Reduce也称为。术语“折叠”在将数组中的每个值滚动或折叠为某种聚合(减少的)最终值时具有直观的意义。我想向您展示迭代过程,而不是解释它
words = %w{cat sheep bear}
words.reduce do |memo, word|
memo.length > word.length ? memo : word
end
变量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'
当前:memo
在reduce
方法=='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`
当前:memo
===“绵羊”
# 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`
Reduce方法结束,因为它是迭代数组完成的。Reduce方法返回设置为“sheep”的memo
希望这不会让你困惑。我想,与其解释,不如展示单个迭代会更好,我想展示迭代过程
words = %w{cat sheep bear}
words.reduce do |memo, word|
memo.length > word.length ? memo : word
end
变量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'
当前:memo
在reduce
方法=='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`
当前: