operations)
It was more a hint to Matz, though I think mwi is off the table
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
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