each with previous?

J

Jari Williamsson

I'm going through lots of data where the result of the current is
affected by the previous element. So what I would need is an
"each_with_previous {|current, prev|}"
...to make it a bit more readable. Is there any built-in Ruby method
that I might have overlooked, or should I build my own?


Best regards,

Jari Williamsson
 
E

Eivind Eklund

I'm going through lots of data where the result of the current is
affected by the previous element. So what I would need is an
"each_with_previous {|current, prev|}"
...to make it a bit more readable. Is there any built-in Ruby method
that I might have overlooked, or should I build my own?

Enumerable.each_cons(2) { |current, prev| ... }

Eivind.
 
G

Gregory Seidman

I'm going through lots of data where the result of the current is affected
by the previous element. So what I would need is an
"each_with_previous {|current, prev|}"
...to make it a bit more readable. Is there any built-in Ruby method that I
might have overlooked, or should I build my own?

You can fake it pretty simply with inject. For example:
[ 1,2,3,4,5 ].inject { |prev,cur| puts "#{prev}, #{cur}"; cur }
1, 2
2, 3
3, 4
4, 5

Note that you do need the block to "return" (i.e. evaluate to) the current
element so that it gets passed into the next iteration.
Best regards,
Jari Williamsson
--Greg
 
R

Rob Biedenharn

I'm going through lots of data where the result of the current is
affected by the previous element. So what I would need is an
"each_with_previous {|current, prev|}"
...to make it a bit more readable. Is there any built-in Ruby method
that I might have overlooked, or should I build my own?

Best regards,

Jari Williamsson


require 'enumerator'


[first, second, third].each_slice(2) do |prev,curr|
# do stuff
end

Except that you don't get a [nil,first] pair to start, you get
[first,second].

You could also do something like:

[first, second, third].inject(nil) do |prev, curr|
# do stuff
curr
end

And curr is assigned to prev in the next iteration and the first
iteration gets [nil,first]

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
P

Phrogz

I'm going through lots of data where the result of the current is
affected by the previous element. So what I would need is an
"each_with_previous {|current, prev|}"
..to make it a bit more readable. Is there any built-in Ruby method
that I might have overlooked, or should I build my own?

Take your pick:

irb(main):001:0> a = %w| a b c d e f g h |
=> ["a", "b", "c", "d", "e", "f", "g", "h"]

irb(main):002:0> require 'enumerator'

irb(main):003:0> a.each_cons(2){ |x,y| puts "#{x}-#{y}" }
a-b
b-c
c-d
d-e
e-f
f-g
g-h

irb(main):004:0> a.each_slice(2){ |x,y| puts "#{x}-#{y}" }
a-b
c-d
e-f
g-h
 
R

Robert Klemme

I'm going through lots of data where the result of the current is affected
by the previous element. So what I would need is an
"each_with_previous {|current, prev|}"
...to make it a bit more readable. Is there any built-in Ruby method that I
might have overlooked, or should I build my own?

You can fake it pretty simply with inject. For example:
[ 1,2,3,4,5 ].inject { |prev,cur| puts "#{prev}, #{cur}"; cur }
1, 2
2, 3
3, 4
4, 5

Note that you do need the block to "return" (i.e. evaluate to) the current
element so that it gets passed into the next iteration.

There is a subtlety: with your code there will be no previous for the
first element. Depending on what the OP needs you can as well do

irb(main):001:0> (1..5).inject(nil) {|prev,curr| p [prev,curr];curr}
[nil, 1]
[1, 2]
[2, 3]
[3, 4]
[4, 5]
=> 5

Jari, what kind of calculation do you do?

Cheers

robert
 
J

Jari Williamsson

Robert said:
There is a subtlety: with your code there will be no previous for the
first element. Depending on what the OP needs you can as well do

irb(main):001:0> (1..5).inject(nil) {|prev,curr| p [prev,curr];curr}
[nil, 1]
[1, 2]
[2, 3]
[3, 4]
[4, 5]
=> 5

Jari, what kind of calculation do you do?

I'll use different approaches for different situations. Starting inject
with nil or first item will both become handy. Thanks!

The tasks are for analyzing musical data. The current note is most often
dependent of previous context.


Best regards,

Jari Williamsson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top