From: David Alan Black Date: 2000-12-18T00:11:34+09:00 Subject: [ruby-talk:7508] Re: flatten_by(n) On Sun, 17 Dec 2000, David Alan Black wrote: > And, what's more.... > > def flatten_once > res = [] > for e in self # a little faster than each > res.push(*e) > end > res > end Correction: def flatten_once res = [] for e in self res += e.to_a # push won't handle *[] (no args) end res end > 10000 iterations: > > user system total real > mine: 1.210000 0.040000 1.250000 ( 1.268839) > yours: 3.040000 0.000000 3.040000 ( 3.042355) It gets a little less dramatic, but still worthwhile: user system total real 1.740000 0.010000 1.750000 ( 1.750059) 3.110000 0.000000 3.110000 ( 3.109317) (using [1,[2,[3],[]], [5],[],[4,[[6]]]]) Can anyone improve on -- or break -- this version? David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav