如何为数组中所有先前的值添加值
假设我有以下数组:
my_array = [1, 5, 8, 11, -6]
我需要迭代该数组并将当前值之前的值添加在一起。举个例子可能会更容易理解。我需要返回一个看起来像这样的数组:
final_array = [1, 6, 14, 25, 19]
我尝试过这样做:
my_array.collect {|value| value + previous_values }
但显然这不起作用,因为我不知道如何获取数组中以前的值。
我是一个编程新手,所以这可能比我做的更容易。我很确定我需要使用收集或注入,但我似乎不知道如何做到这一点。
任何帮助将不胜感激。
Lets say I have the following array:
my_array = [1, 5, 8, 11, -6]
I need to iterate over this array and add the values prior to the current value together. An example will probably be easier to understand. I need to return an array that should look something like this:
final_array = [1, 6, 14, 25, 19]
I have tried doing something like this:
my_array.collect {|value| value + previous_values }
But obviously that doesn't work because I can't figure out how to get the previous values in the array.
I am a programming noob so this might be easier than I am making it. I am pretty sure I need to use either collect or inject, but I can't seem to figure out how to do this.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我的第一直觉是:“这显然是一次扫描(又名前缀求和),所以这应该很容易”:
显然,我最近读了太多的 Haskell 和 Scala,因为 没有
Ruby 中的 Enumerable#scan
...但是:如果您希望 Enumerable#scan 表现得像
Enumerable#reduce
,即采用可选的初始参数并一个可选符号,我们需要使用从 Rubinius 的Enumerable#reduce
窃取的一些参数按摩代码来稍微增强我们的版本:有了这个增强版本,上面的示例现在可以工作了:
如果您再次遇到此类问题,在另一种语言中,请记住术语 scan 和 prefix-sum,此类函数通常非常常见。我不太明白为什么 Ruby 还没有它们。
My very first instinct was: "That's obviously a scan (aka prefix-sum), so that should be easy":
Obviously, I've been reading way too much Haskell and Scala lately, because there is no
Enumerable#scan
in Ruby … yet:If you want
Enumerable#scan
to behave likeEnumerable#reduce
, i.e. take an optional initial argument and an optional symbol, we need to enhance our version slightly with some argument massaging code stolen from Rubinius'sEnumerable#reduce
:With this enhanced version, the example above now works:
If you have this kind of problem again, in another language, remember the terms scan and prefix-sum, such functions are usually pretty common. I don't quite understand why Ruby doesn't have them already.
您自己使用
collect
进行的尝试已经非常接近了;只要继续对之前的值进行求和就可以了。Your own attempt at it with
collect
was already very close; just keep summing the previous values as you go.你可以使用这个:
You can use this:
或者
or
我为此制作了一个预分配结果数组的 gem。即使对于具有大量元素的 Enumerables,操作也非常快。与使用 Enumerable#map 的解决方案不同,其语法与 Enumerable#reduce 的语法完全相同,并且可以选择在幕后使用 Enumerable#reduce,以防您使用猴子修补的 #reduce。该名称取自 Clojure 的同名函数。
https://rubygems.org/gems/reductions
安装:
使用:
I made a gem for this that pre-allocates the result array. The operation is very fast, even for Enumerables with a large number of elements. Unlike solutions using Enumerable#map, the syntax is exactly like that of Enumerable#reduce, and can optionally use Enumerable#reduce under the hood in case you have monkey-patched #reduce. The name was taken from Clojure's function of the same name.
https://rubygems.org/gems/reductions
To install:
To use: