dblack@wobblini.net wrote:
Hi --
Sorry missed a brace on the block. Should be
b = a.inject( ) { |a,e| a << [e,e]}.flatten[1..-2] }
I so much think it's time for flatten to take an argument, specifying
number of levels to flatten. My flattenx extension does this, and it
seems like a natural fit for flatten itself. Otherwise almost all
flatten-based techniques run the risk of over-flattening.
100% agreement here. The problem is easily solved if you ignore the
flattening risk:
a = (1..5).to_a
p a[0..-2].zip(a[1..-1]).flatten
# ==> [1, 2, 2, 3, 3, 4, 4, 5]
The inject solution is a little less elegant and probably much less
efficient:
p a[0..-2].zip(a[1..-1]).inject{|s,x| s+x}
# ==> [1, 2, 2, 3, 3, 4, 4, 5]
Maybe a little more efficient:
p a[0..-2].zip(a[1..-1]).inject{|s,x| s.concat x}
I've gotten out the the habit of using #zip for exactly this reason: you
have to flatten the output to get what you really want, but then you
might get something you really don't want.
···
On Tue, 21 Nov 2006, Daniel N wrote:
On 11/21/06, Daniel N <has.sox@gmail.com> wrote:
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407