From: Bill Clagett Date: 2002-03-19T10:46:34+09:00 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) > -----Original Message----- > From: Joseph McDonald [mailto:joe@vpop.net] > Sent: 18 March, 2002 5:20 pm > To: ruby-talk ML; Marko Schulz > Cc: (ruby-talk ML) > Subject: Re: Wanted: slick rubyesqe algorithm > > > > MS> I don't consider it a slick solution, but it is the one > I came up > MS> with: > > Nice. I shortened it a bit: > > def stringCompress(arr) > newArr = [] > arr.each do |element| > if element.type == String and newArr[-1].type == String > newArr[-1] += element > else > newArr << element > end > end > return newArr > end > > p stringCompress(["AB","CD",:alpha,"DE",:beta,:gamma,"FG","HI"]) > > Hopefully I didn't break it. > -joe >