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!
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.
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
--
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Q: What's the best way to get a really solid knowledge of Ruby?
A: Come to our Ruby training in Edison, New Jersey, September 14-17!
Instructors: David A. Black and Erik Kastner
More info and registration: http://rubyurl.com/vmzN
On Wed, Jul 29, 2009 at 10:50 AM, Xavier Noria<fxn@hashref.com> 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 }
I don't understand that last one. Is this some kind of override of map
that turns it into each_cons + map?
David
--
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Q: What's the best way to get a really solid knowledge of Ruby?
A: Come to our Ruby training in Edison, New Jersey, September 14-17!
Instructors: David A. Black and Erik Kastner
More info and registration: http://rubyurl.com/vmzN
On Wed, Jul 29, 2009 at 2:16 PM, David A. Black<dblack@rubypal.com> wrote:
On Wed, 29 Jul 2009, Robert Dober wrote:
On Wed, Jul 29, 2009 at 10:50 AM, Xavier Noria<fxn@hashref.com> wrote:
David
--
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Q: What's the best way to get a really solid knowledge of Ruby?
A: Come to our Ruby training in Edison, New Jersey, September 14-17!
Instructors: David A. Black and Erik Kastner
More info and registration: http://rubyurl.com/vmzN
--
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.
I confess I don't see the point, but thanks for clarifying.
David
--
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Q: What's the best way to get a really solid knowledge of Ruby?
A: Come to our Ruby training in Edison, New Jersey, September 14-17!
Instructors: David A. Black and Erik Kastner
More info and registration: http://rubyurl.com/vmzN
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
···
On Wed, Jul 29, 2009 at 2:31 PM, David A. Black<dblack@rubypal.com> wrote:
I confess I don't see the point, but thanks for clarifying.
On Wed, Jul 29, 2009 at 2:31 PM, David A. Black<dblack@rubypal.com> 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
--
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Q: What's the best way to get a really solid knowledge of Ruby?
A: Come to our Ruby training in Edison, New Jersey, September 14-17!
Instructors: David A. Black and Erik Kastner
More info and registration: http://rubyurl.com/vmzN
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.
···
On Wed, Jul 29, 2009 at 3:20 PM, David A. Black<dblack@rubypal.com> wrote:
Hi --
On Wed, 29 Jul 2009, Robert Dober wrote:
On Wed, Jul 29, 2009 at 2:31 PM, David A. Black<dblack@rubypal.com> 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.