[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: accessing index inside map

Peña, Botp

7/11/2005 11:12:00 AM

dblack@wobblini wrote:

#> OK, i'll be shooting for the moon here. How about passing a parameter? Is
it
#> then possible to make #each or #map same behavior w #each_with_index or
#> #map_with_index or whatever *_with_index so that the ff works,
#>
#> def each(with_index=true) do... end
#>
#> def map(with_index=true) do ... end
#>
#> and thus, we can do
#>
#> [1,2,3,4,5,6].map(true) {|x,i|[2,5].include?(i) ? x : x*2}
#
#I'd hope not to see that. I'm actually hoping that maybe someday all
#the boolean flag methods (like instance_methods(true) etc.) will be
#changed :-) They've always seemed to me to be the most cryptic thing
#in all of Ruby.
#

Yes, you are correct. But there is some sort of give and take here or
balance if i may say (i think i heard the word balance fr you, sir David)..

How about

[1,2,3,4,5,6].map(WITH_INDEX) {|x,i|[2,5].include?(i) ? x : x*2}
or
[1,2,3,4,5,6].map(with_index=>true) {|x,i|[2,5].include?(i) ? x : x*2}

if we don't pass flags or attribs, wouldn't that make creating additional
methods like

each
each_with_index
map
map_with_index
collect
collect_with_index
...

?

it's with_index es all over..


Also, if we create a with_index method, wouldn't that be invoking another
method just for the index, like

each_with_index.map
or
map.with_index

Wouldn't my algorithm slow down? Does not the chain begs for a single method
-which brings us back to the original question...?


kind regards -botp


#David


5 Answers

Jim Freeze

7/11/2005 12:28:00 PM

0

* "Pea, Botp" <botp@delmonte-phil.com> [2005-07-11 20:12:26 +0900]:

> How about
>
> [1,2,3,4,5,6].map(WITH_INDEX) {|x,i|[2,5].include?(i) ? x : x*2}
> or
> [1,2,3,4,5,6].map(with_index=>true) {|x,i|[2,5].include?(i) ? x : x*2}

How about a more 'rails' like implementation:

[1,2,3].map(:with_index) { |x,ix| ... }
[1,2,3].collect(:with_index) { |x,ix| ... }
[1,2,3].collect!(:with_index) { |x,ix| ... }
[1,2,3].each(:with_index) { |x,ix| ... }
[1,2,3].each { |x| ... }

--
Jim Freeze


daz

7/11/2005 12:57:00 PM

0


Peña, Botp wrote:
>
> [...] wouldn't that make creating additional methods like
>
> each
> each_with_index
> map
> map_with_index
> collect
> collect_with_index
> ..
>
> ?
>
> it's with_index es all over..
>

It is. None are required; not even each_with_index.

>
> Also, if we create a with_index method, wouldn't that be invoking another
> method just for the index, like
>
> each_with_index.map
> or
> map.with_index
>
> Wouldn't my algorithm slow down? Does not the chain begs for a single method
> -which brings us back to the original question...?
>

Yes. I'm taking sedatives while it's seriously being considered that
an Array should become an Enumerator in order to use its #with_index method
on an intermediate #map object to produce ... an Array ... instead of the
regular #map method with an optional index.

I would have posted a link to the ruby-core thread, but it was my
first post to a mailing list and my MAIL settings were different
from my NEWS settings, so it's messed up (quoted-printable MIME).

Here's a text version (minor EDITs). I'm sure it'll be relevant to you.

http://www.d10.karoo.net/ruby/xv/ruby-cor...

You'll see examples using bang! methods.

(( I'd be fascinated to see map!.with_index or map.with_index!
produce anything intelligible using require 'enumerator'. ))


>
> kind regards -botp
>

daz



dblack

7/11/2005 5:55:00 PM

0

dblack

7/11/2005 5:59:00 PM

0

Eric Hodel

7/11/2005 11:27:00 PM

0


On 11 Jul 2005, at 05:27, Jim Freeze wrote:

> How about a more 'rails' like implementation:
>
> [1,2,3].map(:with_index) { |x,ix| ... }
> [1,2,3].collect(:with_index) { |x,ix| ... }
> [1,2,3].collect!(:with_index) { |x,ix| ... }
> [1,2,3].each(:with_index) { |x,ix| ... }
> [1,2,3].each { |x| ... }

Ugh. My impression has always been that they couldn't make up their
minds, or, didn't think hard enough about picking good names the
first time around.

--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04