collect with index?

T

Tim Conner

simple question: how can I do a collect with index? This should be
like each_with_index but should return a new array containing the values
returned by the block.
i.e
Say i want to return:
["cat number 1","dog number 2","pig number 3"

how would i do this? I am thinking that there must be something along
the lines of:
%w{"cat","dog","pig"}.collect_with_index {|animal,index| animal+" number
"+index}

but I can't find a 'collect_with_index' in the documentation. How would
I do this?
Thanks
 
S

Sebastian Hungerecker

Tim said:
simple question: =C2=A0how can I do a collect with index?

In 1.8: require 'enumerator'; foo.to_enum:)each_with_index).collect {|x,i| }
In 1.9: foo.collect.with_index {|x,i| }

HTH,
Sebastian
=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 
S

Stefan Lang

2008/3/5 said:
simple question: how can I do a collect with index? This should be
like each_with_index but should return a new array containing the values
returned by the block.
i.e
Say i want to return:
["cat number 1","dog number 2","pig number 3"

how would i do this? I am thinking that there must be something along
the lines of:
%w{"cat","dog","pig"}.collect_with_index {|animal,index| animal+" number
"+index}
require "enumerator" => true
ary = ["cat", "dog", "pig"] => ["cat", "dog", "pig"]
ary.enum_for:)each_with_index).collect { |animal, index|
"#{animal} number #{index + 1}"
}
=> ["cat number 1", "dog number 2", "pig number 3"]

Or in Ruby 1.9 it's simply:
ary = ["cat", "dog", "pig"] => ["cat", "dog", "pig"]
ary.each_with_index.collect { |animal, index|
"#{animal} number #{index + 1}"
}
=> ["cat number 1", "dog number 2", "pig number 3"]


HTH,
Stefan
 
R

Rick DeNatale

simple question: how can I do a collect with index? This should be
like each_with_index but should return a new array containing the values
returned by the block.
i.e
Say i want to return:
["cat number 1","dog number 2","pig number 3"

how would i do this? I am thinking that there must be something along
the lines of:
%w{"cat","dog","pig"}.collect_with_index {|animal,index| animal+" number
"+index}

but I can't find a 'collect_with_index' in the documentation. How would
I do this?

require 'enumerator'
%w(cat dog pig).enum_with_index.collect {|e,i| "#{e} number #{i+1}"}
=> ["cat number 1", "dog number 2", "pig number 3"]
 
W

William James

simple question: how can I do a collect with index?

You mean "map with index".
This should be
like each_with_index but should return a new array containing the values
returned by the block.
i.e
Say i want to return:
["cat number 1","dog number 2","pig number 3"

how would i do this? I am thinking that there must be something along
the lines of:
%w{"cat","dog","pig"}

That should be
%w(cat dog pig)
.collect_with_index

That should be
..map_with_index
{|animal,index| animal+" number
"+index}

but I can't find a 'collect_with_index' in the documentation. How would
I do this?
a = %w(cat dog pig)
==>["cat", "dog", "pig"]
a.zip( (1..a.size).to_a ).map{|x,y| "#{x} #{y}" }
==>["cat 1", "dog 2", "pig 3"]
 
J

James Gray

You mean "map with index".

Why? collect() is an alias for map()? Some favor one, some the other.

There was no problem with the request.

James Edward Gray II
 
W

William James

Why? collect() is an alias for map()? Some favor one, some the other.

Some actually favor drinking urine. That doesn't make it right.
"map" describes what is done; "collect" does not, and is consequently
misleading.

What if "pick_and_choose" were an alias for "map"? Would it make
sense
to use it? No, it wouldn't. It doesn't describe what is being done.

"map" is shorter and clearer. There's every reason to use it and no
reason not to use it. Just as "junior" is preferable to
"the second".
There was no problem with the request.

Then there's no problem with "junior".
 
J

James Gray

Some actually favor drinking urine. That doesn't make it right.
"map" describes what is done; "collect" does not, and is consequently
misleading.

Well it's obvious that at least myself and the creator of Ruby disagree.

James Edward Gray II
 
T

Todd Benson

Then there's no problem with "junior".

So James Gray III would be what, James Gray, Junior Junior? Or like
Great Junior? I'm pretty sure the numbering is continued practice
that was used mostly for political reasons. Royal families during
Newton's time, for example, had strange marriages, and it was good to
keep track of someone's lineage, especially if you have the same name
floating around ;) This isn't just a western concept, either.

I agree, #map makes more sense, but the use of #collect doesn't bother
me that much. I think I would get irritated, however, if I saw both
in the same code.

Todd
 
M

Mark Bush

Todd said:
I agree, #map makes more sense, but the use of #collect doesn't bother
me that much. I think I would get irritated, however, if I saw both
in the same code.

In functional languages, I'm happily a mapper, but in OO languages I'm
an avid collector. I guess it's the Smalltalker in me.

I wouldn't baulk at mixing and matching according to conditions. I'd
probably #map a variable representing a Proc object and #collect a
block:

f = lambda {|x| ...}
a.map &f

a.collect {|x| ...}

If that makes me weird then I'm happy to be weird.
 
J

James Gray

In functional languages, I'm happily a mapper, but in OO languages I'm
an avid collector. I guess it's the Smalltalker in me.

I wouldn't baulk at mixing and matching according to conditions. I'd
probably #map a variable representing a Proc object and #collect a
block:

f = lambda {|x| ...}
a.map &f

a.collect {|x| ...}

If that makes me weird then I'm happy to be weird.

I like weird people. ;)

Weird is just another way to say "free thinker," anyway.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,330
Latest member
AlvaStingl

Latest Threads

Top