From: Bill Clagett Date: 2002-03-19T14:03:34+09:00 Subject: RE: Wanted: slick rubyesqe algorithm On second thought, it could be shorter and probably shouldn't be destroy the incoming array... def weird_compress2 from, to=[] return to if from.size == 0 t, from = from[0], from[1..-1] t = (to.pop + t) if (to.last.is_a? String) && (t.is_a? String) weird_compress2 from, to.push t end a = [ "AB","CD",:alpha,"DE",:beta,:gamma,"FG","HI"] p weird_compress2(a) Clear as mud. bill > -----Original Message----- > From: Bill Clagett [mailto:bill@32768.com] > Sent: 18 March, 2002 5:47 pm > To: ruby-talk ML > Subject: RE: Wanted: slick rubyesqe algorithm > > > I took a slightly different tack. Does ruby know about tail > recursion? > > bill > > def weird_compress from, to=[] > return to if from.size == 0 > t = from.shift > if (to.last.is_a? String) && (t.is_a? String) > to.push to.pop + t > else > to.push t > end > weird_compress from, to > end > > a = [ "AB","CD",:alpha,"DE",:beta,:gamma,"FG","HI"] > p weird_compress(a) >