OO Challenge

I’ll throw in my version. Logically it is structured very much like Robert
Klemme’s except that I made lighter weight choices for some of the
abstractions. For example, I use simple procs for the tax groups and my
TaxPayer is Struct based. What is really interesting is that despite the
differences in representation, the last bits of our programs are identical
except for minor spelling differences.

Here’s the code.

···

Soldier = proc { |p| p.salary / 10.0 }
Professor = proc { |p| p.salary / 15.0 + 100 }

TaxPayer = Struct.new(:salary, :tax_classes)
class TaxPayer
def tax
tax_classes.collect { |tc| tc.call(self) }.max
end
end

taxpayers = [
TaxPayer.new(3000, [Soldier])
TaxPayer.new(5000, [Professor])
TaxPayer.new(6000, [Professor, Soldier])
]

Taxpayers.each { |tp| printf “%0.2f\n”, tp.tax }

Disclaimer: I don’t believe this example says anything deep or significant
about OO verse non-OO.


– Jim Weirich / Compuware
– FWP Capture Services
– Phone: 859-386-8855

“Weirich, James” James.Weirich@FMR.COM schrieb im Newsbeitrag
news:1C8557C418C561429998C1F8FBB283A728BA48@MSGDALCLB2WIN.DMN1.FMR.COM

I’ll throw in my version. Logically it is structured very much like
Robert
Klemme’s except that I made lighter weight choices for some of the
abstractions. For example, I use simple procs for the tax groups and my
TaxPayer is Struct based. What is really interesting is that despite
the
differences in representation, the last bits of our programs are
identical
except for minor spelling differences.

Probably there’s not too much room for variations with this small example.
Or we think along the same lines. Could be interesting to see with what
others come up.

Rereading the original posting I think the author of these lines has not
yet fully understand OO:

“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.”

In fact there are only few languages in which an instance can change its
class at runtime. I’m not sure about belonging to several classes at the
same time. Even if there were, I’d be curios how that should be modelled
(after all you have to invoce all calculation methods before deciding on
the return value.)

But that doesn’t necessarily mean that OO languages are not well suited
for this problem. In fact, any of the ruby solutions looks cleaner to me
than the VB examples. My 2 cent…

Here’s the code.
[snip]

Great short implementation! I always like to see how small programs can
get in Ruby. Unfortunately that reminds me that I sometimes do seem to
make things unnecessary complicated… sigh

OTOH we don’t know what these groups are supposed to do other than
calculating the tax value. They sure have some weird code that uniquely
identifies… :slight_smile:

Disclaimer: I don’t believe this example says anything deep or
significant about OO verse non-OO.

Very much indeed so. I still wonder why the OP did not put the original
link here - if there is any at all…

Regards

robert

Weirich, James wrote:

I’ll throw in my version. Logically it is structured very much like
Robert Klemme’s except that I made lighter weight choices for some of the
abstractions. For example, I use simple procs for the tax groups and my
TaxPayer is Struct based. What is really interesting is that despite the
differences in representation, the last bits of our programs are identical
except for minor spelling differences.

Here’s the code.
def tax
tax_classes.collect { |tc| tc.call(self) }.max
end
I like James’ solution best because he has tax_classes 'collect’ing - which
seems to model the real world well :).

– Richard

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).

···

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


– Jim Weirich jweirich@one.net http://onestepback.org

“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)

Link is above there somewhere.

(great it was still in my clipboard!)

The article was about Eiffel.
But the guy blasted OO …
I know from experience that the least you know - the more you think you
know :stuck_out_tongue:
So obviously that guy was whizzing on an ego trip.

He fails to realize that OO is to encapsulate and reduce complexity for
larger programs - not trivial examples.

His example is silly.
As OO is usually built on procedural-like code.
Now … if he were to have an example involving a more complex application
Then he would lose ground - he is cheating, setting the rules - with an example of
20 lines or so.

As I stated I tried VB.NET because you can do illegal things
that makes code more sugary-like.

With C# you can make classes morph into others - while introducing some
ugly parameters. (Perhaps) the solution could have been more concise.

But I was aiming for the solution to be readable when declaring.
That is IMO … what OO is for. Work hard to make it easier.

I was close to finding a solution - but no EVAL in VB, and didn’t want to
thread at MSDN for hours, … I got sick of Visual Studio .NET slow,
crawling once more -hanging on “Wait 20 minutes …” (P-OFF MS! grrrr).

Refreshing to be here under Linux again :slight_smile:

Thanks again.

I will look into RUBY more
I am looking for a nice language.
I tried Smalltalk but couldnt get round it. I feel even embarrased about
it :frowning:
Ruby seems to be more my style!

I deffy want to re-write a site which I began with C#.
So any advice on RUBY (ie links) for rendering pages and dealing with Response Fields
is much appreciated.

Of course I will always be searching through posts here trying to learn as much as
poss.

All the best

Henry Gilbert

“Henry Gilbert” nospam@alliancetec.com schrieb im Newsbeitrag
news:pan.2003.09.12.22.03.43.604791@alliancetec.com

He fails to realize that OO is to encapsulate and reduce complexity for
larger programs - not trivial examples.

I think the posted examples show that even simple programs can benefit
from OO (and especially Ruby) - at least by increased readability but also
by reduced code (you get polymorphism and a lot of other stuff for free).

I deffy want to re-write a site which I began with C#.
So any advice on RUBY (ie links) for rendering pages and dealing with
Response Fields
is much appreciated.

Look out for eruby, amrita, apache mod-ruby and the like.

Of course I will always be searching through posts here trying to learn
as much as
poss.

Go ahead, you’ll find plenty. :slight_smile:

Regards

robert