David Alan Black <dblack / candle.superlink.net> wrote: > >Hello -- > >On Fri, 15 Dec 2000, Ben Tilly wrote: > > (map & grep) > > In Ruby the two concepts are orthogonal. I don't yet know > > if you can have a function that returns different numbers > > of elements different times. Certainly the first thing I > > tried to do with map was return 2 elements for 1. (Looking > > at a case where I know current production versions of Perl > > doesn't scale.) I couldn't see how to do it. And grep > > itself cannot be used with arbitrary blocks. (Therefore > > it is truer to the actual name of the function!) > >(I remember being relieved when I discovered find_all, after trying >to get grep to act like Perl's grep.) > >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. Think for a second of the "array with indices" map that someone did. That could be as simple as ary.each_index.map do |i| i, ary[i] end But my current guess is that Ruby doesn't have the list-oriented nature of Perl, nor is there an easy way to make it fit. Which is a feature I will miss, but I have to admit that simply because it was such an unusual feature of Perl, it is rarely stressed. Authors of books don't realize what a unifying concept it is. Students don't know to look for it. So while I find it useful, I don't know that it fits with the principle of least surprise. (Yeah, I know how to imitate a list-oriented nature by always working with through an array. But that is a bad hack.) Cheers, Ben _____________________________________________________________________________________ Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com