From: Ben Tilly Date: 2000-12-15T23:09:54+09:00 Subject: [ruby-talk:7317] Re: Why isn't Perl highly orthogonal? David Alan Black wrote: >On Fri, 15 Dec 2000, Ben Tilly wrote: > > > David Alan Black wrote: > > > > > > > > > >[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! > >I'm not sure what it is you're saying wouldn't work. For example: > >a = [1,2,3,4,5] >b = a.map do |e| [e,-e] end >c = b.map_with_indices do |e,i| "element #{i} is #{e.inspect}" end > >p c[2] # => "element 2 is [3, -3]" I was thinking of the following: a = [1, [], [3,4,[]]] Pass that to flatten and you will get [1,3,4]. which is likely not what someone with that data structure intended to do! >The .flatten was only to illustrate (one version of) map (with a >little help :-) producing an array with more elements than the input >array. (Are we talking past each other on some aspect of this?) I think so. I know how to get an array to wind up with more elements than it started with. But the flatten method is unlikely to do what you wanted. Incidentally I did discover an amusing gotcha in Ruby that comes from the implicit recursion in a lot of functions. a = [] a.push a a <=> a Having thought about it, I think this is an appropriate response. [...] >Yes and no. I probably should have written "In fact" rather than >"Then again", above. My first line of thought was: since the method >syntax turns a,b into [a,b] anyway, it's no big deal that we have to >write [a,b] in the iterator block. But I do wonder now why the same >a,b syntax isn't allowed in such a block (where it would return [a,b]) > -- no implications of non-Ruby-esque list behavior). I don't have enough of a sense yet to even try to say what Ruby-esque behaviour should be. Sorry. Cheers, Ben _________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com