From: Logan Capaldo Date: 2005-12-07T14:00:02+09:00 Subject: Re: Good Ruby Examples? On Dec 6, 2005, at 12:47 PM, gabriele renzi wrote: > Wayne Vucenic ha scritto: >> Hi Hampton, >>> What I'm asking you guys for is if you have any short code ideas >>> that >>> can be programmed *really* quickly in Ruby that might illustrate its >>> versatility. Some code example that would be hell to write in other >>> languages that would be interesting for an hour long explaination. >> My favorite is the well-known quicksort algorithm: >> def quicksort(a) >> return a if a.size <= 1 >> pivot = a[0] >> quicksort(a.select {|i| i < pivot }) + >> a.select {|i| i == pivot } + >> quicksort(a.select {|i| i > pivot }) >> end >> quicksort is "hell" to write in C or C++ unless you're extremely >> clever. > > you may appreciate Enumerable#partition: > def qs(a) > return a if a.size <=1 > pivot = a.shift > less, more = a.partition{|y| y < pivot} > qs(less) + [pivot] + qs(more) > end > > > I don't know, if you start throwing partition in there, you're this close to just saying a.sort