Dear Ruby Community,
One day I hope to learn your trade
But before that …
A reasonably arrogant guy came out blasting in a web-forum about how OO is
crap and all that.
He offered a challenge which I tried to defeat but in VB.NET
Now VB.NET sucks simply because the Visual Studio IDE that comes with it
is all very boring, slow, bureacratic, clunky and when you get a spark of
inspiration it hangs saying “Please wait while we update your Help File”
(for 20 minutes)
Pure Joy-Killer
His challenge is as follows:
- I wont give the URL yet because I want you guys to come with the best
solution, don’t worry I wont take credit for it - will post the website
and say this is the RUBY’s solution loggin in as Anonymous *
[QUOTE]
Whole OO is step back in programming. Encapsulation is trivial
and unnecessary, inheritance wrong and polymorphism weak …
OK, here is my favorite ‘Tax Payer’ challenge for OO languages. There are lot of different groups of people with different rules for tax calculation. One man can be in the same time member of many groups. His membership can change during program execution time. If he is member of many groups in the time tax is calculated, his tax is the greatest one on the base of all groups he is member (they are clever!). Natural situation, isn’t it? Now, (1) define separate functions that calculate tax for each groups, and (2) write polymorphic function that calculate tax for tax payer, no matter of his membership to one or many groups in the same time. Here is code in procedural language: record tax_payer(salary,member_of)
[Version 1]
procedure tax_soldier(X)
return X.salary/10.0
end
procedure tax_professor(X)
return X.salary/15.0+100
end
#many of them …
procedure tax(X) #procedural polymorphism
result:=0
every class:=!X.member_of do
if result then result:=t
return result
end
procedure main()
B:=tax_payer(5000,[“professor”])
C:=tax_payer(6000,[“professor”,“soldier”])
#…
write(tax(A),tax(B),tax©) #this line is goal
end
[Added to Version 1]
Oh, board ignored part of the procedure tax(), here it is again …
procedure tax(X)
result:=0
every class:=!X.member_of do
if result less_than t:=proc(“tax_”||class)(X) then result:=t
return result
end
[Version 2]
Here is the procedural version of your second program:
record tax_payer(salary,member_of)
record tax_rule_data(multiplier, additional)
global tax_rule
procedure tax(payer)
res:=0
every r:=tax_rule[! payer.member_of ] do
if res less_than t:=payer.salary/r[1]+r[2]
then res:=t
return res
end
procedure main()
tax_rule:=table()
tax_rule[“soldier”]:=tax_rule_data(10,0)
tax_rule[“professor”]:=tax_rule_data(15,100)
write(tax(A))
end
That is actual code in Icon, not pseudocode, just like my previous program.
I believe I have my point about elegance.
I do not think that Tax Payer problem deserves to be called ‘functional problem’ - it is simple problem every general programming language should be able to solve easily and with elegance. Such problems are everywhere. It is not exotic automated theorem proving problem … neither is proc() non-procedural… actually, I remember Sinclair ZX 81 had similar GO SUB (expression) statement. Sure, it is slower, but worked well even then, on 1000 times slower comp and 20 000 times smaller memory.
Tax Payer problem is all about object (tax_payer) - class (soldier …) membership and polimorphic functions, just it is not that simple as designers of OO languages believe (object is member of only one class (and superclasses) and does not change its membership, function version can be decided solely on class membership) - hence one is forced to handle membership relation on his own, and OO support integrated in the language is shown to be restrictive and cluttering. Proof is in the puding - compare Icon and Java code.
It is not the only problem with OO. Inheritance rules are even worse; almost everything in that concept is wild guess, and code is always much worse.
If anyone disagree and he think he has problem where OO is better, i accept the challenge.
[/UNQUOTE]
So sorry for the abuse in pasting.
Now I started solving this in VB.NET
But just to be vindictive lol … wanted to most beautiful solution ever.
Which I believe RUBY can deliver.
This guy is talking crap.
My version in VB would be pure syntatic sugar.
But its fundamental we had an eval function
ie eval “3 + 4 / 7” returns 1
I tried to import that from JScript Library
And “bureacratic” Visual Studio started asking me to reflect on which
Engine, for which I tried to look into the help files: and it began
pissing me off with insert CD this, wait until we update your files … and
a second time wait until we update your file, now insert CD number 2 …
[note when I first installed I explicitily asked VS to loads ALL Library
and also saved the Library Locally but VS is just thick]
My version should have a Main more or less like:
Sub Main()
Dim ListOfTaxRules As New TaxRules
Dim ListOfTaxPayers As New TaxPayers
Dim Member As Object
With ListOfTaxTules
.Add("soldier","/10")
.Add("teacher","/15+2000")
.Add("professor","/12+1000")
End WIth
With ListOfTaxPayers
.Add("John","soldier",30000)
.Add("Mary","teacher",15000)
.Add("Gary",{"professor","soldier"},50000)
End With
For Each Member As Object In ListOfTaxPayers.List
Console.WriteLine(Member.Name,Member.CalculatedTax)
Next
End Sub
Of course I am abusing the VB.NET as Much as possible.
For the sake of readability and winning the challenge
I haven’t supplied code for the Classes because I got stuck on importing
the Eval function from Microsoft.Jscript Library.
Which would also tarnish the “elegant” solution
There are many ways round to this problem - and the most concise and yet beautiful
easy to read … should be considered.
So please venture completely different routes - so we can breed the best
one.
Of course his challenge is stupid, because OO languages excel on a large
scale, not on such simple things. But still I am confident there is a
solution to his silly challenge.
Thanks for your attention.
Would love to learn from you all RUBY sometime.
For another abandoned .NET project - an English Online Website
I was going to build for free - and being GPL
(Any takers?
All the best
Henry Gilbert
···
A:=tax_payer(3000,[“soldier”])
A:=tax_payer(1000,[“soldier”,“professor”])