David Alan Black <dblack / candle.superlink.net> wrote: > >On Fri, 15 Dec 2000, Ben Tilly wrote: > > > David Alan Black <dblack / candle.superlink.net> wrote: > > > > > >As for map mapping two(or more)-for-one, I've done it, when needed, > > >along these lines: > > > > > >[1,2,3,4,5].map do |e| [e,-e] end .flatten > > > => [1, -1, 2, -2, 3, -3, 4, -4, 5, -5] > > > > > I thought of that. But if you want your output to be an > > array of arrays, it doesn't work right. > >Just don't .flatten it: > >[1,2,3,4,5].map do |e| [e,-e] end > => [[1, -1], [2, -2], [3, -3], [4, -4], [5, -5]] > >(I thought the flattened version was more what you'd meant, >but anyway, either is possible.) Um, what if you want to do your array with indices map on an array like the above? .flatten is recursive, which means that a list of list of list of lists turns into a single list! Anyways, you could do it correctly with the following ugly method. Class Array def flatten_one() out # Do I need this? Need to check scoping rules for i in self do for j in i do out.push j end end out end end > > Think for a second of the "array with indices" map that > > someone did. > >That was me :-) Wasn't sure. > > That could be as simple as > > > > ary.each_index.map do |i| i, ary[i] end > >You've introduced the list syntax here (and I do understand that it >isn't meant as a working example), but you've also turned each_index >from an iterator into the equivalent of: > > (0...ary.size).to_a > >Probably not worth it :-) Actually, my quest for map_to_indices >was brought about in part by dislike of the look of this: > > (0...ary.size).map do |i| ... end > >which was the only way I could think of to iterate through indices and >return a mapping. Anyway, as for the > > i, ary[i] > >part, I'm not sure why that can't be the return (exit?) value of an >iterator block, as it can for method definitions. Then again... when >you > > return a,b > >from a method, you are returning [a,b]. > You answered your own question I think. :-) I don't think that Ruby is list-oriented or that list-oriented ways of thinking are a good fit for it. Having lists with variable numbers of arguments is not part of the basic design and I don't think should be hacked on top when it doesn't fit conceptually. Not a big deal. Cheers, Ben _____________________________________________________________________________________ Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com