# Previous value in array block

**URL:** <https://www.ruby-forum.com/t/previous-value-in-array-block/172864>\
**Category:** Ruby\
**Created:** [July 29, 2009, 5:39am UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864 "2009-07-29T05:39:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 5:39am UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/1 "2009-07-29T05:39:18Z")

</div>

Is this a good way to use a previous value in an array block?

I want to subtract the item(i) from item(i-1) of the array, a and return  
a new array of the results. Below is my attempt.

a = [1,3,3,2]  
prev\_item = 0  
b = []

a.each do |item|  
b \<\< item - prev\_item  
prev\_item = item  
end

puts b

=\> 1,2,0,-1

I’m thinking there must be a more elegant way of doing this.

thank you!

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 6:03am UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/2 "2009-07-29T06:03:39Z")

</div>

On Wed, Jul 29, 2009 at 11:39 AM, Jason  
Lillywhite[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> b \<\< item - prev\_item  
> prev\_item = item  
> end
> 
> puts b
> 
> =\> 1,2,0,-1
> 
> I’m thinking there must be a more elegant way of doing this.

to me, your code is elegant enough.  
not sure if making code shorter would make it more, so..

> a = [1,3,3,2]  
> =\> [1, 3, 3, 2]  
> a.zip(a.dup.unshift(0)).map{|x,y| x-y}  
> =\> [1, 2, 0, -1]

kind regards -botp

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 6:10am UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/3 "2009-07-29T06:10:06Z")

</div>

Jason L. wrote:

> a = [1,3,3,2]  
> prev\_item = 0  
> b =
> 
> a.each do |item|  
> b \<\< item - prev\_item  
> prev\_item = item  
> end
> 
> puts b
> 
> =\> 1,2,0,-1

Another way could be:

a = [1,3,3,2]  
b = [a[0]]

for i in 1…(a.length)  
b \<\< (a[i] - a[i-1])  
end

puts b

Not sure if that can be considered elegant though. 🙂

-DR

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 8:51am UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/4 "2009-07-29T08:51:38Z")

</div>

> I want to subtract the item(i) from item(i-1) of the array, a and return  
> a new array of the results. Below is my attempt.

But, you are doing the opposite 🙂  
Anyway, based on your code,

FWIW, here is another one-liner.  
Elegant? I don’t know.

b = Array.new(a.length){|i| a[i]-a[i-1]}[1..-1].unshift(a[0])

Harry

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 9:45am UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/5 "2009-07-29T09:45:28Z")

</div>

On Wed, Jul 29, 2009 at 8:18 AM, Harry K.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Elegant? I don’t know.
> 
> b = Array.new(a.length){|i| a[i]-a[i-1]}[1..-1].unshift(a[0])

Another way:

irb(main):001:0\> a = [10,20,30,40]  
irb(main):014:0\> b =   
=\>   
irb(main):015:0\> a.each\_with\_index {|x,y| b \<\< (y \> 0? (x - a[y-1]): x)}  
=\> [10, 20, 30, 40]

irb(main):019:0\> b  
=\> [10, 10, 10, 10]

Jesus.

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 10:51am UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/6 "2009-07-29T10:51:02Z")

</div>

each\_cons seems natural here:

require ‘enumerator’

a = [1, 3, 3, 2]  
b = a[0, 1]

a.each\_cons(2) { |x, y| b \<\< y - x }

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 12:57pm UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/7 "2009-07-29T12:57:29Z")

</div>

Hi –

On Wed, 29 Jul 2009, Xavier N. wrote:

> each\_cons seems natural here:
> 
> require ‘enumerator’
> 
> a = [1, 3, 3, 2]  
> b = a[0, 1]
> 
> a.each\_cons(2) { |x, y| b \<\< y - x }

Here’s a 1.9 way, probably guilty of gross wasteage (by creating an  
extra new array) but kind of cool:

[0,\*a].each\_cons(2).map {|x,y| y - x }

David

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 2:12pm UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/8 "2009-07-29T14:12:24Z")

</div>

On Wed, Jul 29, 2009 at 10:50 AM, Xavier N.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> each\_cons seems natural here:
> 
> require ‘enumerator’
> 
> a = [1, 3, 3, 2]  
> b = a[0, 1]
> 
> a.each\_cons(2) { |x, y| b \<\< y - x }  
> and purely functional  
> ( [0] + a ).each\_cons( 2 ).map{ |x,y| y -x }  
> or with Tom’s map arity trick implemented  
> ( [0] + a ).map{ | x, y | y - x }

–  
Toutes les grandes personnes ont d’abord été des enfants, mais peu  
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 2:13pm UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/9 "2009-07-29T14:13:34Z")

</div>

On Wed, Jul 29, 2009 at 12:56 PM, David A. Black[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > a.each\_cons(2) { |x, y| b \<\< y - x }
> 
> Here’s a 1.9 way, probably guilty of gross wasteage (by creating an  
> extra new array) but kind of cool:
> 
> [0,\*a].each\_cons(2).map {|x,y| y - x }  
> A beauty, sorry did not see it before my post!  
> Cheers  
> Robert

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 10:05am UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/10 "2009-07-29T10:05:57Z")

</div>

Jason L. wrote:

> b \<\< item - prev\_item  
> prev\_item = item  
> end
> 
> puts b
> 
> =\> 1,2,0,-1
> 
> I’m thinking there must be a more elegant way of doing this.
> 
> thank you!

I was recently reading through the facets docuementation and so thought  
I would develop a solution for the OP using the elementwise method that  
facets supplies. However while other facets methods from enumerable are  
available, elementwise does not seem to be.

C:\Documents and Settings\Administrator\>irb  
irb(main):001:0\> a=Array.new  
=\>   
irb(main):002:0\> a.respond\_to?(“elementwise”)  
=\> false  
irb(main):003:0\> a.respond\_to?(“entropy”)  
=\> false  
irb(main):004:0\> require “facets”  
=\> true  
irb(main):005:0\> a.respond\_to?(“entropy”)  
=\> true  
irb(main):006:0\> a.respond\_to?(“elementwise”)  
=\> false

I am using windows xp, one click Ruby 186-26 and facets 2.5.0.

Can some kind person point out my error?

Thanks

Steve

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/11 "2009-07-29T14:17:05Z")

</div>

Hi –

On Wed, 29 Jul 2009, Robert D. wrote:

> ( [0] + a ).each\_cons( 2 ).map{ |x,y| y -x }  
> or with Tom’s map arity trick implemented  
> ( [0] + a ).map{ | x, y | y - x }

I don’t understand that last one. Is this some kind of override of map  
that turns it into each\_cons + map?

David

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/12 "2009-07-29T14:22:21Z")

</div>

On Wed, Jul 29, 2009 at 2:16 PM, David A. Black[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > a = [1, 3, 3, 2]  
> > > that turns it into each\_cons + map?  
> > > IIRC it was like this

module Enumerable  
method\_alias : **map** , map  
def map &blk  
return **map** (&blk) if blk.arity \< 2  
each\_cons( blk.arity).map(&blk)  
…

Robert

> 

–  
Toutes les grandes personnes ont d’abord été des enfants, mais peu  
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 2:32pm UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/13 "2009-07-29T14:32:18Z")

</div>

Hi –

On Wed, 29 Jul 2009, Robert D. wrote:

> > > >
> > 
> > I don’t understand that last one. Is this some kind of override of map  
> > that turns it into each\_cons + map?  
> > IIRC it was like this
> 
> module Enumerable  
> method\_alias : **map** , map  
> def map &blk  
> return **map** (&blk) if blk.arity \< 2  
> each\_cons( blk.arity).map(&blk)  
> …

I confess I don’t see the point, but thanks for clarifying.

David

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 2:43pm UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/14 "2009-07-29T14:43:26Z")

</div>

On Wed, Jul 29, 2009 at 2:31 PM, David A. Black[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I confess I don’t see the point, but thanks for clarifying.  
> It is cheating, bending Ruby to the needs of the problem. We even had  
> a discussion if it should use each\_cons or each\_slice below the hood.

But the point might be, that it might make sense to have things like  
map\_cons or map\_slice  
I can only think about Ruby code, writing it, sorry.  
Gotta look into Facets for that.

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 2:48pm UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/15 "2009-07-29T14:48:07Z")

</div>

On Wed, Jul 29, 2009 at 8:11 PM, Robert D.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> or with Tom’s map arity trick implemented  
> ( [0] + a ).map{ | x, y | y - x }

very cool.  
thanks for the tip.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/16 "2009-07-29T14:52:13Z")

</div>

On Wed, Jul 29, 2009 at 8:11 AM, Robert D.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ( [0] + a ).each\_cons( 2 ).map{ |x,y| y -x }  
> Here’s a lisp/scheme inspired approach using inject and with an array  
> serving as a “cons cell” and first =\> car and last =\> cdr:

[1, 3, 3, 2].inject([0,]) { |memo, elem| [elem, memo.last \<\< (elem

- memo.first)] }.last # =\> [1, 2, 0, -1]

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [NameBright - Coming Soon](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 3:20pm UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/17 "2009-07-29T15:20:55Z")

</div>

Hi –

On Wed, 29 Jul 2009, Robert D. wrote:

> On Wed, Jul 29, 2009 at 2:31 PM, David A. Black[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I confess I don’t see the point, but thanks for clarifying.  
> > It is cheating, bending Ruby to the needs of the problem. We even had  
> > a discussion if it should use each\_cons or each\_slice below the hood.
> 
> But the point might be, that it might make sense to have things like  
> map\_cons or map\_slice

In 1.9 you get: enumerable.each\_cons(x).map … which has much the  
same effect. I don’t like the idea of map having a stealth each\_cons  
in it. Also, defining it for Enumerable won’t affect arrays (at least  
in MRI), because Array overries map.

David

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 29, 2009, 4:54pm UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/18 "2009-07-29T16:54:35Z")

</div>

On Wed, Jul 29, 2009 at 3:20 PM, David A. Black[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > But the point might be, that it might make sense to have things like  
> > map\_cons or map\_slice
> 
> In 1.9 you get: enumerable.each\_cons(x).map … which has much the  
> same effect. I don’t like the idea of map having a stealth each\_cons  
> in it. Also, defining it for Enumerable won’t affect arrays (at least  
> in MRI), because Array overries map.

yes we should not confuse, playing around and changing the language. I  
have sometimes the feeling that people get nervous when I do this  
“cheating” stuff, but if I meant this to be in Ruby I would say so and  
probably at Ruby core. No it is just for the fun and elegance.  
R.

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 30, 2009, 8:11am UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/19 "2009-07-30T08:11:20Z")

</div>

On Jul 28, 10:39 pm, Jason L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## b \<\< item - prev\_item
> 
> Posted viahttp://www.ruby-forum.com/.

Never forget: we don’t need no stinkin’ loops!

x = 2,3,5,8,13  
p x.zip( [0] + x ).map{|a,b| a-b}

=\> [2, 1, 2, 3, 5]

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [July 30, 2009, 8:12am UTC](https://www.ruby-forum.com/t/previous-value-in-array-block/172864/20 "2009-07-30T08:12:30Z")

</div>

On Jul 28, 10:39 pm, Jason L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## b \<\< item - prev\_item
> 
> Posted viahttp://www.ruby-forum.com/.

Never forget: we don’t need no stinkin’ loops!

x = 2,3,5,8,13  
p x.zip( [0] + x ).map{|a,b| a-b}

=\> [2, 1, 2, 3, 5]
