Contionnal sum

Discussion in 'Ruby' started by Fernando Perez, May 15, 2008.

  1. Hi,

    I am wondering if it is possible to create a conditionnal sum.

    For instance I would like to do something like this:

    @total_amount = @orders.sum { |order| order.amount if order.paid == true
    }
    --
    Posted via http://www.ruby-forum.com/.
     
    Fernando Perez, May 15, 2008
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Fernando Perez wrote:
    | Hi,
    |
    | I am wondering if it is possible to create a conditionnal sum.
    |
    | For instance I would like to do something like this:
    |
    | @total_amount = @orders.sum { |order| order.amount if order.paid == true
    | }

    Should be.

    Something like this (untested, and unsafe, probably not even correct):

    def conditional_sum order, condition
    ~ order.each do |o|
    ~ sum = sum + o unless o.paid?
    ~ end
    ~ return sum
    end

    I made the idea explicit, so that you should at least get the idea.

    - --
    Phillip Gawlowski
    Twitter: twitter.com/cynicalryan
    Blog: http://justarubyist.blogspot.com

    You! What PLANET is this!
    ~ -- McCoy, "The City on the Edge of Forever", stardate 3134.0
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.8 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iEYEARECAAYFAkgsZ9UACgkQbtAgaoJTgL8L2gCeLZyAQcn6+o+4McD7qQLJlL1o
    qEUAn0JrnuPs7XVBNUYAEuCd6gUIsjBO
    =k8YY
    -----END PGP SIGNATURE-----
     
    Phillip Gawlowski, May 15, 2008
    #2
    1. Advertising

  3. Fernando Perez

    Xavier Noria Guest

    On Thu, May 15, 2008 at 6:26 PM, Fernando Perez <> wrote:

    > I am wondering if it is possible to create a conditionnal sum.
    >
    > For instance I would like to do something like this:
    >
    > @total_amount = @orders.sum { |order| order.amount if order.paid == true
    > }


    sum... is that Rails? If that's the case you could filter them before:

    # untested
    @total_sum = @orders.select(&:paid).sum(&:amount)

    Although AR has another SQL-based #sum that may be worth exploring if
    @orders are ARs.

    That being said, perhaps you weren't aware of the fact that
    Enumerable#sum is defined by Active Support, but let me point out that
    those extensions are discussed in the very Rails mailing list.

    -- fxn
     
    Xavier Noria, May 15, 2008
    #3
  4. Fernando Perez

    Ken Bloom Guest

    Xavier Noria <> wrote:
    > On Thu, May 15, 2008 at 6:26 PM, Fernando Perez <> wrote:
    >
    >> I am wondering if it is possible to create a conditionnal sum.
    >>
    >> For instance I would like to do something like this:
    >>
    >> @total_amount = @orders.sum { |order| order.amount if order.paid == true
    >> }

    >
    > sum... is that Rails? If that's the case you could filter them before:
    >
    > # untested
    > @total_sum = @orders.select(&:paid).sum(&:amount)
    >
    > Although AR has another SQL-based #sum that may be worth exploring if
    > @orders are ARs.
    >
    > That being said, perhaps you weren't aware of the fact that
    > Enumerable#sum is defined by Active Support, but let me point out that
    > those extensions are discussed in the very Rails mailing list.


    Enumerable#sum is also in facets.
    The .select method is a good way, but you can use less memory by doing

    @orders.sum {|order| order.paid? order.amount : 0 }

    Facets' #sum doesn't skip nils, so the method you proposed won't work.

    --Ken

    --
    Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
     
    Ken Bloom, May 15, 2008
    #4
  5. * Fernando Perez <> (18:26) schrieb:

    > Hi,
    >
    > I am wondering if it is possible to create a conditionnal sum.
    >
    > For instance I would like to do something like this:
    >
    > @total_amount = @orders.sum { |order| order.amount if order.paid == true
    > }


    @total_amount = @orders.inject(0) do | sum, order |
    order.paid ? sum + order.amount : sum
    end

    or

    @total_amount = @orders.select { | o | o.paid }. sum

    BTW: Comparing a boolean value to true or false only shows you don't
    understand booleans.

    And if that order class is yours: I'd rather call the accessor »paid?«.

    mfg, simon .... l
     
    Simon Krahnke, May 15, 2008
    #5
  6. Thank you all for your messages. Yes it is a piece of code I am using in
    a Rails app. I thought it would fit better in this list rather than the
    Rails one, as the problem is not specific to Rails.

    > BTW: Comparing a boolean value to true or false only shows you don't

    understand booleans.

    Yeah I knew someone would nail me on this. I wanted to make my problem
    clear, so that's why I specified this silly "== true". In my real code,
    I don't have it trust me. If I hadn't put it, someone would have come up
    with "hey wtf is that order.paid thing!?"


    > The .select method is a good way, but you can use less memory by doing
    >
    > @orders.sum {|order| order.paid? order.amount : 0 }


    This is fantastic, I knew the solution was super simple. Day after day I
    love Ruby more and more.

    Best regards, and once again thank you very much for assisting me.

    --
    Posted via http://www.ruby-forum.com/.
     
    Fernando Perez, May 15, 2008
    #6
    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. Roger Moore
    Replies:
    0
    Views:
    750
    Roger Moore
    Jul 7, 2003
  2. Davisro
    Replies:
    4
    Views:
    793
    Bruno Sirianni
    Apr 10, 2004
  3. Davisro
    Replies:
    2
    Views:
    6,616
    Davisro
    Apr 20, 2004
  4. Guest

    Datagrid sort & sum question

    Guest, Jun 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    372
    =?Utf-8?B?QmhhcmF0IEJpeWFuaQ==?=
    Jul 31, 2004
  5. Sam

    DataGrid? Sum Calculate?

    Sam, Jan 21, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,580
    Eliyahu Goldin
    Jan 21, 2005
Loading...

Share This Page