when looping, using each, i find i am often wonton of a few nice
"sugers" myself.
first iteration - do this first thing, but never again
last iteration - do this last (same as else i think)
not last iteration - do for every iteration BUT the last (this one
would be very useful!!!
not first - you get the idea
an automatic index counter - i’m doing an arr.each but i also need a
count too.
and of course the few mentioned in the for…each…end thread
concerning breaks or success (though i don’t find these as useful as the
above)
but there’s no way in hell we can get all these things into blocks as we
know them today. perhaps there’s a new kind of looping contruct waiting
to be phathomed by us that can do all this (and more?)
~transmi
iterate x in arr
do
…
when _i == 1 # first
…
when _i == -1 # last
…
when _i != -1 # all but last
…
when _i != 1 # all but first
…
do
…
when _i == nil # if arr is empty? and perhaps nil? (else?)
…
ensure # on break and success?
…
end
_i is the automatic index (in this case _i started with 1, but probably
0 is better. of course its useable in the code,) notice the do’s, which
like the when’s, can go in any order. the loop is proccessed in order
dependent on the when conditions. do’s of course have no conditions. and
there’s no reason other conditions can’t be used based on other
variables.
did i miss anything? well you get the idea. what do you think?
each_with_nindex, rocking! i’ll throw that in my library, for sure.
it’s really nice for building strings with dividers, e.g. “a,b,c” in
order to keep that last comma from getting appended to the end.
I like each_with_nindex too… but show me an example of this comma
problem that isn’t better solved with #join and I’ll put the price of
a beer in your Paypal account
I like each_with_nindex too… but show me an example of this comma
problem that isn’t better solved with #join and I’ll put the price
of a beer in your Paypal account
Heh, that’s what I was thinking – what about #join?
I like each_with_nindex too… but show me an example of this comma
problem that isn’t better solved with #join and I’ll put the price of
a beer in your Paypal account
what i used to do:
q = ‘’
arr.each do |x|
q << ‘junk’ << x << ‘morejunk’ << ‘,’
end
q.chomp!(‘,’)
what i now do:
q = ‘’
arr.each_with_nindex do |x, n|
q << ‘junk’ << x << ‘morejunk’
q << ‘,’ if n != -1
end
how do you do it with join?
note: how i used to do it is probably a tad faster actually
~transami
···
On Thu, 2002-07-11 at 04:55, David Alan Black wrote:
On Thu, 2002-07-11 at 04:55, David Alan Black wrote:
Hi –
On Thu, 11 Jul 2002, Tom Sawyer wrote:
each_with_nindex, rocking! i’ll throw that in my library, for sure.
it’s really nice for building strings with dividers, e.g. “a,b,c” in
order to keep that last comma from getting appended to the end.
I like each_with_nindex too… but show me an example of this comma
problem that isn’t better solved with #join and I’ll put the price of
a beer in your Paypal account
what i now do:
q = ‘’
arr.each_with_nindex do |x, n|
q << ‘junk’ << x << ‘morejunk’
q << ‘,’ if n != -1
end
how do you do it with join?
note: how i used to do it is probably a tad faster actually
Hmm, how about something like…
arr.collect { |x| ‘junk’ + x + ‘morejunk’ }.join(“,”)
?
Seems a little cleaner/clearer, but that’s IMHO of course Your
method is probably more efficient, since the one I mentioned creates
an intermediate array. I guess it depends on how performance critical
the code in question is.
----- Original Message -----
From: “Josh Huber” huber@alum.wpi.edu
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, July 11, 2002 8:58 AM
Subject: Re: spliter of for…each: looping
I like each_with_nindex too… but show me an example of this comma
problem that isn’t better solved with #join and I’ll put the price
of a beer in your Paypal account
Heh, that’s what I was thinking – what about #join?
Seems a little cleaner/clearer, but that’s IMHO of course Your
method is probably more efficient, since the one I mentioned creates
an intermediate array. I guess it depends on how performance critical
the code in question is.
actually joins faster. just tested it out. also discovered that:
“junk#{x}morejunk” is much faster then “junk” << x << “morejunk”
thanks for all the help guys! i think i can speed up miter a bit just by
using join.
what i now do:
q = ‘’
arr.each_with_nindex do |x, n|
q << ‘junk’ << x << ‘morejunk’
q << ‘,’ if n != -1
end
how do you do it with join?
note: how i used to do it is probably a tad faster actually
Hmm, how about something like…
arr.collect { |x| ‘junk’ + x + ‘morejunk’ }.join(“,”)
?
Seems a little cleaner/clearer, but that’s IMHO of course Your
method is probably more efficient, since the one I mentioned creates
an intermediate array. I guess it depends on how performance critical
the code in question is.
–
Josh Huber
–
~transami
“They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.”
– Benjamin Franklin
q = ‘’
arr.each_with_nindex do |x, n|
q << ‘junk’ << x
q << ‘morejunk’ if x == ‘more’
q << ‘,’ if n != -1
end
Sure:
arr.collect {|x| “junk#{x}#{‘more’ if x==‘more’}” }.join “,”
That’s in the nature of #collect (aka #map): you’re basically
translating from your original array to a new array of the same
length, using what happens in the block (one element at a time) as the
translator or transformer. And “what happens in the block” can
certainly be conditional on the value of the element(s).
By the way, another way to do the all-but-last thing with an array is: