Conditional block operations

Is there a way to filter results of EACH or COLLECT functions? Let’s say
in

file.each do |line|
a, b = line.split(pattern)
End

I wanna work it just over not empty lines or lines fitting some pattern.

Anotehr question is about COLLECT;

myarray.collect {|x| x + ", "}

I wanna execute only over specific emelents (let’s say only odd ones or
all but last).

I know how to do both of them in a couple of lines but RUBY impresses my
by its brevity. Will it do it in this case too?

Roman Rytov

Is there a way to filter results of EACH or COLLECT functions? Let’s say
in

file.each do |line|
a, b = line.split(pattern)
End

I wanna work it just over not empty lines or lines fitting some pattern.

file.readlines.select{ |x| x =~ pattern }.each{ |line| a,b = line.split(pattern) }

Anotehr question is about COLLECT;

myarray.collect {|x| x + ", "}

I wanna execute only over specific emelents (let’s say only odd ones or
all but last).

myarray.select{ |x| x % 2 == 1 }.collect{ |x| x + ", " } #.join here ?

myarray[0…-1].collect{ |x| x + ", " } # .join ??

···

On Tue, Dec 03, 2002 at 10:58:15PM +0900, Roman Rytov wrote:

I know how to do both of them in a couple of lines but RUBY impresses my
by its brevity. Will it do it in this case too?

Roman Rytov


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

  • liw prefers not to have Linus run Debian, because then /me would
    have to run Red Hat, just to keep the power balance :slight_smile:
    #Debian

Roman Rytov wrote:

Anotehr question is about COLLECT;

myarray.collect {|x| x + ", "}

I wanna execute only over specific emelents (let’s say only odd ones or
all but last).

what about:
filteredArray = myarray.find_all { |x| (x%2==0) }
filteredArray.collect…
(see also Enumerable#reject)

but that’s still two lines.

Roman Rytov

emmanuel

···


“If someone breaks into my house and steals my CDs,
who calls the cops, me or the music industry?
If it’s me, then that’s my property.”
–Ruben Safir

Hi –

Anotehr question is about COLLECT;

myarray.collect {|x| x + ", "}

I wanna execute only over specific emelents (let’s say only odd ones or
all but last).

I know how to do both of them in a couple of lines but RUBY impresses my
by its brevity. Will it do it in this case too?

You need to get your shift key fixed :slight_smile: There’s no RUBY, just Ruby
and ruby.

As for collect’ing selectively, if the elements you don’t want
are at a known distance from either end of the array, you can do:

myarray[0…-2].collect {…} # up to 2nd-to-last element

etc.

Otherwise I think you have to do some kind of pre-filtering. If we had
map_with_indices, you could do:

myarray.map_with_indices {|e,i| e + ", " if i % 2 == 0} .compact

but we don’t. (Hint, hint.)

However, you could do:

(0…myarray.size).map {|i| myarray[i] + ", " if i % 2 == 0} .compact

That’s one-line-ish, anyway :slight_smile:

David

···

On Tue, 3 Dec 2002, Roman Rytov wrote:


David Alan Black
exiled to ISP shell account due to recalcitrant ISP :frowning:

That’s going to work only on numerical elements… which the
collect clause then won’t work on. I think Roman meant odd-numbered
indices (?). (Which doesn’t explain why I used even-numbered
indices in my example – oh well :slight_smile:

David Black

···

On Tue, 3 Dec 2002, Mauricio [iso-8859-1] Fernández wrote:

On Tue, Dec 03, 2002 at 10:58:15PM +0900, Roman Rytov wrote:

Anotehr question is about COLLECT;

myarray.collect {|x| x + ", "}

I wanna execute only over specific emelents (let’s say only odd ones or
all but last).

myarray.select{ |x| x % 2 == 1 }.collect{ |x| x + ", " } #.join here ?

[snipped]

Otherwise I think you have to do some kind of pre-filtering. If we had
map_with_indices, you could do:

myarray.map_with_indices {|e,i| e + ", " if i % 2 == 0} .compact

but we don’t. (Hint, hint.)

#!/usr/bin/env ruby

module Enumerable
def map_with_indices
a = # should this be “self.class.new”?
each_with_index { |o, i| a << yield(o, i) }
a
end

for those unfamiliar with the nuances of the english language

alias_method :map_with_indexes, :map_with_indices
end

if FILE == $0

test arrays

a = [1, 1, 2, 3, 5, 8, 13, 21]
puts ‘a:’, a.join(‘,’),
‘a.map_with_indices:’, a.map_with_indices { |o, i| o + i }.join(‘,’),
‘a.map_with_indexes:’, a.map_with_indexes { |o, i| o + i }.join(‘,’)
end

···

a:
1,1,2,3,5,8,13,21
a.map_with_indices:
1,2,4,6,9,13,19,28
a.map_with_indexes:
1,2,4,6,9,13,19,28

Merry christmas!


Paul Duncan pabs@pablotron.org pabs in #gah (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

I took it too literally :slight_smile: “only odd elements”. And you’re right about #+
not working then…

As there’s no select_with_index (nor select_index), this is the shortest
thing I can think of right now:

v =
myarray.each_with_index { |x,i| v << x if i % 2 == 1 }
v.collect{ |x| "#{x}, "}

···

On Tue, Dec 03, 2002 at 11:57:48PM +0900, David A. Black wrote:

On Tue, 3 Dec 2002, Mauricio [iso-8859-1] Fernández wrote:

On Tue, Dec 03, 2002 at 10:58:15PM +0900, Roman Rytov wrote:

Anotehr question is about COLLECT;

myarray.collect {|x| x + ", "}

I wanna execute only over specific emelents (let’s say only odd ones or
all but last).

myarray.select{ |x| x % 2 == 1 }.collect{ |x| x + ", " } #.join here ?

That’s going to work only on numerical elements… which the
collect clause then won’t work on. I think Roman meant odd-numbered
indices (?). (Which doesn’t explain why I used even-numbered
indices in my example – oh well :slight_smile:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

*** PUBLIC flooding detected from erikyyy
THAT’s an erik, pholx… :wink:
– Seen on #LinuxGER

Because it’s each_with_index

alias_method :map_with_index, :map_with_indices

– Nikodemus

···

On Thu, 5 Dec 2002, Paul Duncan wrote:

def map_with_indices
a = # should this be “self.class.new”?
each_with_index { |o, i| a << yield(o, i) }
a
end

for those unfamiliar with the nuances of the english language

alias_method :map_with_indexes, :map_with_indices

I posted a hack to generalise this a while back:
http://groups.google.com/groups?selm=6s5F9.908926%24v53.33379576%40news3.calgary.shaw.ca&rnum=24

It could, of course, be generalised still further to allow ‘indexes’ and
‘indices’ as well as index.

martin

···

Paul Duncan pabs@pablotron.org wrote:

#!/usr/bin/env ruby

module Enumerable
def map_with_indices
a =3D # should this be “self.class.new”?
each_with_index { |o, i| a << yield(o, i) }
a
end

for those unfamiliar with the nuances of the english language

alias_method :map_with_indexes, :map_with_indices
end

Hi –

···

On Thu, 5 Dec 2002, Paul Duncan wrote:

[snipped]

Otherwise I think you have to do some kind of pre-filtering. If we had
map_with_indices, you could do:

myarray.map_with_indices {|e,i| e + ", " if i % 2 == 0} .compact

but we don’t. (Hint, hint.)

#!/usr/bin/env ruby

module Enumerable
def map_with_indices
a = # should this be “self.class.new”?
each_with_index { |o, i| a << yield(o, i) }
a
end

It was more a hint to Matz, though I think mwi is off the table :slight_smile: As
for the self.class.new thing, I think it should be an array, since
that’s what #map returns.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

operations)

It was more a hint to Matz, though I think mwi is off the table :slight_smile:

I’m not sure it’s off the table forever… Matz
did not seem opposed to it, just unconvinced.

Here’s an exchange from Nov 18 – Matz is quoted,
then (I guess) you, then me…

map_with_index was rejected just because no one proposed its typical
usage. YANGI principle applied.

Hmmmm… I’ll try to think of some examples. It certainly seems to
me to be at least as useful as each_with_index, potentially (i.e., if
it existed :slight_smile:

I’d go so far as to say that map_with_index
does for map exactly what each_with_index
does for each.

I do find it useful.

So I’d say (unless Matz says otherwise): Create a
rationale for its existence and submit again.

Here’s a little contrived example of how I’d show
the relationship between ewi and mwi.

Problem: Take two arrays a and b, known to be the
same size and contain numbers, and produce an array
c which is the sum of the corresponding elements.

Hmm, can’t use a.each; must index into the other
array. Need each_with_index.

c =
a.each_with_index {|x,i| c << x + b[i]}

Now, same problem, but we want to modify a in-place
rather than creating c.

a.map_with_index! {|x,i| x + b[i]}

And in fact, with mwi (non-bang) the first problem
also can be solved:

c = a.map_with_index {|x,i| x + b[i]}

No doubt David or someone can come up with a more
compelling example.

Hal

···

----- Original Message -----
From: dblack@candle.superlink.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, December 04, 2002 8:47 PM
Subject: Re: Enumerable#map_with_indices (was Re: Conditional block

From: dblack@candle.superlink.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, December 04, 2002 8:47 PM
Subject: Re: Enumerable#map_with_indices (was Re: Conditional block
operations)

It was more a hint to Matz, though I think mwi is off the table :slight_smile:

I’m not sure it’s off the table forever… Matz
did not seem opposed to it, just unconvinced.

Here’s an exchange from Nov 18 – Matz is quoted,
then (I guess) you, then me…

map_with_index was rejected just because no one proposed its typical
usage. YANGI principle applied.

Hmmmm… I’ll try to think of some examples. It certainly seems to
me to be at least as useful as each_with_index, potentially (i.e., if
it existed :slight_smile:

I’d go so far as to say that map_with_index
does for map exactly what each_with_index
does for each.

I do find it useful.

So I’d say (unless Matz says otherwise): Create a
rationale for its existence and submit again.

Here’s a little contrived example of how I’d show
the relationship between ewi and mwi.

Problem: Take two arrays a and b, known to be the
same size and contain numbers, and produce an array
c which is the sum of the corresponding elements.

Hmm, can’t use a.each; must index into the other
array. Need each_with_index.

c =
a.each_with_index {|x,i| c << x + b[i]}

Now, same problem, but we want to modify a in-place
rather than creating c.

a.map_with_index! {|x,i| x + b[i]}

And in fact, with mwi (non-bang) the first problem
also can be solved:

c = a.map_with_index {|x,i| x + b[i]}
c = (0…a.size).map {|i| a[i] + b[i]}

I think that matz wasn’t really convinced by this sort of examples, and
IIRC he’d normally give such a solution w/ map and state that YANGNI.

No doubt David or someone can come up with a more
compelling example.

At any rate, I’d take mwi for completeness sake. Of course I might not
really need it, but it looks more natural to me than using map and I’d
no longer have NameErrors when trying to call “map_with_index” (cause I
really expected it to be there!)

···

On Thu, Dec 05, 2002 at 12:03:29PM +0900, Hal E. Fulton wrote:

----- Original Message -----


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Let’s call it an accidental feature.
–Larry Wall