OO Challenge

Discussion in 'Ruby' started by Henry Gilbert, Sep 12, 2003.

  1. 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 *

     
    Henry Gilbert, Sep 12, 2003
    #1
    1. Advertising

  2. Henry Gilbert

    Robert Feldt Guest

    Henry Gilbert <> skrev den Fri, 12 Sep 2003 21:37:04 +0900:

    > 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)
    >     A:=tax_payer(1000,["soldier","professor"])
    >     write(tax(A))
    > end
    >

    Without any refactoring or rethinking of his "problem" here
    is a Ruby version:

    Rule = Struct.new("Rule", :multiplier, :additional)
    class Rule
    def calc_tax(salary)
    salary/(multiplier || 1) + (additional || 0)
    end
    end

    Rules = {"soldier" => Rule.new(10.0, 0), "professor" => Rule.new(15.0, 100)}

    TaxPayer = Struct.new("TaxPayer", :salary, :member_of)
    class TaxPayer
    def tax
    Rules.select {|n,r| member_of.include?(n)}.map do |profession, rule|
    rule.calc_tax(salary)
    end.max end
    end

    a = TaxPayer.new(1000, ["soldier", "professor"])
    p a.tax # => 166.666666667

    Not the way I would structure this but its close to his code.
    Which one is more "elegant" is subjective IMHO but I don't think
    he has got a strong case... ;)

    It also seems like a lousy example for comparisons between languages
    and paradigms. Then again most such comparisons are a lousy idea... ;)

    Regards,

    Robert
     
    Robert Feldt, Sep 12, 2003
    #2
    1. Advertising

  3. "Robert Feldt" <> schrieb im Newsbeitrag
    news:eek:...
    [snip]

    I prefer this approach since it is more OO in the sense that rules and
    their names are better bound together.

    robert


    require 'set'

    # abstract base, not really needed
    class TaxGroup
    class << self
    attr_reader :all_groups

    def inherited(gr)
    $stderr.puts "Group #{gr}"
    ( @all_groups ||= Set.new ) << gr
    end
    end
    end

    # tax groups
    class TaxGroupSoldier < TaxGroup
    def calc_tax(emp)
    emp.salary/10.0
    end
    end

    class TaxGroupProf < TaxGroup
    def calc_tax(emp)
    emp.salary/15.0+100
    end
    end

    # tax payers
    class TaxPayer
    attr_reader :tax_groups
    attr_accessor :salary

    def initialize(sal, groups = [])
    @salary = sal
    @tax_groups = Set.new
    @tax_groups.merge groups
    end

    def tax()
    self.tax_groups.map do |gr|
    gr.new.calc_tax(self)
    end.max
    end
    end

    # example
    taxPayers = [
    TaxPayer.new(3000,[TaxGroupSoldier]),
    TaxPayer.new(5000,[TaxGroupProf]),
    TaxPayer.new(6000,[TaxGroupProf, TaxGroupSoldier]),
    ]

    taxPayers.each do |p|
    puts "#{p.inspect}: #{p.tax}"
    end
     
    Robert Klemme, Sep 12, 2003
    #3
  4. Henry Gilbert

    Eric Hodel Guest

    --/GPgYEyhnw15BExa
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    Henry Gilbert () wrote:

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


    Ok... Different groups of people

    > One man can be in the same time member of many groups.


    Ok... A person is in one or more groups

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


    Ok... A person calculates his tax based on which groups he is in.

    > 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)
    > =20
    > Tax Payer problem is all about object (tax_payer) - class (soldier
    > ...) membership and polimorphic functions,


    In most of the Ruby solutions, the person is the one calculating
    the tax, the tax groups don't calculate how much tax a person owes.

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


    A person is in one or more tax groups, which suggests that the
    person should hold which professions they are in, not the other way
    around. (With a trivial bit of work, you could have the profession
    hold all the people who are in that profession as well, but that is not
    useful for the example proposed.)

    > 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 you have to fight your language, you're doing something wrong. The
    easy way is probably the right way. The real problem here seems to be
    that the poster doesn't understand how to model problems in OO.

    --=20
    Eric Hodel - - http://segment7.net
    All messages signed with fingerprint:
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


    --/GPgYEyhnw15BExa
    Content-Type: application/pgp-signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (FreeBSD)

    iD8DBQE/YgQMMypVHHlsnwQRAlOnAKCq78so5q4US/Y7Xm6a8Dzjk+nWxQCZAX3H
    wJvXFR9PdnmcbHwW4DUUdGE=
    =nMz9
    -----END PGP SIGNATURE-----

    --/GPgYEyhnw15BExa--
     
    Eric Hodel, Sep 12, 2003
    #4
  5. Henry Gilbert

    Sean O'Dell Guest

    Henry Gilbert wrote:
    > 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 *
    >
    >
     
    Sean O'Dell, Sep 12, 2003
    #5
  6. Here is the LINK !!!

    On Fri, 12 Sep 2003 13:31:00 +0100, Henry Gilbert wrote:

    > 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 *
    >
    >
     
    Henry Gilbert, Sep 12, 2003
    #6
  7. Henry Gilbert, Sep 12, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Artco News

    Parsing challenge...

    Artco News, Oct 7, 2003, in forum: Perl
    Replies:
    6
    Views:
    479
    Ara.T.Howard
    Oct 8, 2003
  2. Artco News

    Parsing challenge...

    Artco News, Oct 7, 2003, in forum: Perl
    Replies:
    2
    Views:
    425
    Ed Morton
    Oct 7, 2003
  3. .Net Newbie
    Replies:
    0
    Views:
    470
    .Net Newbie
    Jul 29, 2004
  4. Random

    XML challenge!

    Random, Nov 16, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,293
    Marshal Antony
    Nov 16, 2004
  5. Raj Thakkar
    Replies:
    8
    Views:
    561
    Patrick Olurotimi Ige
    Dec 9, 2004
Loading...

Share This Page