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.)

> Think for a second of the "array with indices" map that
> someone did.  

That was me :-)

> 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].


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav