[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: OO Challenge

Jim Weirich

9/13/2003 2:27:00 AM

On Fri, 2003-09-12 at 10:57, Weirich, James wrote:

Not to beat a dead horse or anything, but I love playing with variations
on a theme...

I reviewed the thread that Henry posted. Not much interesting except
that one fellow posted a Haskell version of the tax program. Here it is
...

tax_soldier earning = earning/10
tax_professor earning = earning/15 + 100

tax (TaxPayer earning taxgroups) =
maximum (map (\f -> f earning) taxgroups)

-? tax (TaxPayer 3000 [tax_soldier, tax_professor])

Wow, short and to the point. So, could a Ruby version be that concise?
Here's my attempt ...

TaxSoldier = proc { |earning| earning / 10.0 }
TaxProfessor = proc { |earning| earning / 15.0 + 100 }

def tax(earning, tax_groups)
tax_groups.collect { |g| g.call(earning) }.max
end

puts tax(3000, [TaxSoldier, TaxProfessor])

Pretty much a line for line translation. Why so much shorter than
previous versions? Because we threw out the tax payer abstraction and
just pass around earning (e.g. salary).

--
-- Jim Weirich jweirich@one.net http://onest...
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)