Iteration the ruby way

Lähettäjä: "Robert Klemme" <bob.news@gmx.net>
Aihe: Re: iteration the ruby way

"E S" <eero.saynatkari@kolumbus.fi> schrieb im Newsbeitrag
news:20050206143902.PIMQ15813.fep31-app.kolumbus.fi@mta.imail.kolumbus.fi...
>> Lähettäjä: Kaspar Schiess <eule@space.ch>
>> Aihe: Re: iteration the ruby way
>>(In response to news:1107566961.943586.81760@g14g2000cwa.googlegroups.com
> by William James)
>>> Shouldn't that be
>>>
>>> ary[0...-1].each {|e| do_something(e) }
>>> do_something_else(ary.last)
>>>
>>> or
>>>
>>> ary[0..-2].each {|e| do_something(e) }
>>> do_something_else(ary.last)
>>>
>>> ?
>>
>>I would prefer this solution for its clarity of expression.
>
> Me too :slight_smile: I just think the #inject won on the neat-o factor,
> particularly since it seemed adequate for the OP.

Partly yes and partly no: apart from the cute factor, there is a real
advantage of the inject solutions show: they work for *all* Enumerables
while the array indexing works only for arrays. So #inject is more general.
Also, Array# creates a new array instance along the way which is a bit
inefficient. But I do agree that the indexing might be more readable.

I think assuming an Array is valid in the described scenario (which
is the last element in a Hash, after all?) #inject certainly works
for the OP's scenario, but if the goal is to apply a different method
on the last element or even just to detect the last element, it is not
as intuitive as indexing or an explicit #last. In addition, #each and
#map provide different semantics for further clarity.

Kind regards

    robert

E

···

Hi --

···

On Mon, 7 Feb 2005, E S wrote:

"Robert Klemme" <bob.news@gmx.net>
Aihe: Re: iteration the ruby way

"E S" <eero.saynatkari@kolumbus.fi> schrieb im Newsbeitrag
news:20050206143902.PIMQ15813.fep31-app.kolumbus.fi@mta.imail.kolumbus.fi...

Lähettäjä: Kaspar Schiess <eule@space.ch>
Aihe: Re: iteration the ruby way
(In response to news:1107566961.943586.81760@g14g2000cwa.googlegroups.com

by William James)

Shouldn't that be

ary[0...-1].each {|e| do_something(e) }
do_something_else(ary.last)

or

ary[0..-2].each {|e| do_something(e) }
do_something_else(ary.last)

?

I would prefer this solution for its clarity of expression.

Me too :slight_smile: I just think the #inject won on the neat-o factor,
particularly since it seemed adequate for the OP.

Partly yes and partly no: apart from the cute factor, there is a real
advantage of the inject solutions show: they work for *all* Enumerables
while the array indexing works only for arrays. So #inject is more general.
Also, Array# creates a new array instance along the way which is a bit
inefficient. But I do agree that the indexing might be more readable.

I think assuming an Array is valid in the described scenario (which
is the last element in a Hash, after all?)

If you goal was to create a string representation of the hash, with
some kind of marker between each key/value pair -- but not after the
last one -- then the last-ness of the last element would be important
in the sense that you'd treat it differently (whatever it was, and
whether or not it remained the same from one run to another).

David

--
David A. Black
dblack@wobblini.net

"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag news:Pine.LNX.4.61.0502061257450.13178@wobblini...
Hi --

"Robert Klemme" <bob.news@gmx.net>
Aihe: Re: iteration the ruby way

"E S" <eero.saynatkari@kolumbus.fi> schrieb im Newsbeitrag
news:20050206143902.PIMQ15813.fep31-app.kolumbus.fi@mta.imail.kolumbus.fi...

Lähettäjä: Kaspar Schiess <eule@space.ch>
Aihe: Re: iteration the ruby way
(In response to news:1107566961.943586.81760@g14g2000cwa.googlegroups.com

by William James)

Shouldn't that be

ary[0...-1].each {|e| do_something(e) }
do_something_else(ary.last)

or

ary[0..-2].each {|e| do_something(e) }
do_something_else(ary.last)

?

I would prefer this solution for its clarity of expression.

Me too :slight_smile: I just think the #inject won on the neat-o factor,
particularly since it seemed adequate for the OP.

Partly yes and partly no: apart from the cute factor, there is a real
advantage of the inject solutions show: they work for *all* Enumerables
while the array indexing works only for arrays. So #inject is more general.
Also, Array# creates a new array instance along the way which is a bit
inefficient. But I do agree that the indexing might be more readable.

I think assuming an Array is valid in the described scenario (which
is the last element in a Hash, after all?)

If you goal was to create a string representation of the hash, with
some kind of marker between each key/value pair -- but not after the
last one -- then the last-ness of the last element would be important
in the sense that you'd treat it differently (whatever it was, and
whether or not it remained the same from one run to another).

Note though that you might as well treat the *first* element specially. IMHO that's usually easier even with #each:

first = true
enum.each do |e|
  if first
    first = false
  else
    print " -- "
  end
  print e
end
print "\n"

Kind regards

    robert

···

On Mon, 7 Feb 2005, E S wrote:

Though I hate doing this, because that 'if', that you know is going to be
useless after the first time, is nonetheless run for every subsequent
element. I guess in terms of actually running time it's negligible, but
it still feels ugly.

martin

···

Robert Klemme <bob.news@gmx.net> wrote:

Note though that you might as well treat the *first* element specially.
IMHO that's usually easier even with #each:

first = true
enum.each do |e|
  if first
    first = false
  else
    print " -- "
  end
  print e
end
print "\n"

"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:jfXNd.318521$6l.10705@pd7tw2no...

>
> Note though that you might as well treat the *first* element

specially.

> IMHO that's usually easier even with #each:
>
> first = true
> enum.each do |e|
> if first
> first = false
> else
> print " -- "
> end
> print e
> end
> print "\n"

Though I hate doing this, because that 'if', that you know is going to

be

useless after the first time, is nonetheless run for every subsequent
element. I guess in terms of actually running time it's negligible, but
it still feels ugly.

Yes, totally agree. I just wanted to show a more readable solution than
the #inject variants to show that treating the first element specially
might be easier or at least as easy as treating the last element
specially.

Kind regards

    robert

···

Robert Klemme <bob.news@gmx.net> wrote: