From: "Mauricio Fernández" Date: 2002-12-05T18:32:43+09:00 Subject: Re: Enumerable#map_with_indices (was Re: Conditional block operations) On Thu, Dec 05, 2002 at 12:03:29PM +0900, Hal E. Fulton wrote: > ----- Original Message ----- > From: > To: "ruby-talk ML" > 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 :-) > > 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]} 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!) -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Let's call it an accidental feature. --Larry Wall