How can I create a new array for summing array elements in place in Ruby?
[1,2,3,4,5].each_cons(2).map {|a, b| a + b }
gives me [3, 5, 7,
[3, 5, 7,
Another variation:
> [1, 2, 3, 4, 5].reduce([]) {|acc, el| acc << el + (acc[-1] || 0); acc} #=> [1, 3, 6, 10, 15]
Yuk.