Skip to content

Add a base argument to reduce #2629

Open

Description

@AtnNn

The base argument to reduce was removed (see #888), with default being the recommended work-around.

default is not appropriate because it catches too many errors. For example:

>>> r.expr([{n: 1}, {}]).reduce(lambda a, b: a['n'] + b['n']).default(0)
0

seq.reduce(f, base=b) should be equivalent to

  • b if seq is empty
  • f(seq.reduce(f), b) otherwise

Activity

  1. neumino commented on Jul 1, 2014

    @neumino
    Member

    Shouldn't we have a catch method instead?

    r.expr([{n: 1}, {}]).reduce(lambda a, b: a['n'] + b['n'])
        .catch(r.emptySequence, 0)
        .catch(r.missingField, r.error("A document is malformed"))
    

    That kind of looks like the way promises work in JavaScript https://github.com/petkaantonov/bluebird/blob/master/API.md#catchfunction-errorclassfunction-predicate-function-handler---promise

  2. AtnNn commented on Jul 1, 2014

    @AtnNn
    MemberAuthor

    @neumino That still catches too many errors. You can currently do:

    sequence.reduce(f).default(lambda e: r.branch(e == "Cannot reduce over an empty stream", 0, r.error()))
    

    But if the sequence is not empty and the call to f throws that same error, it is going to be caught by the handler.

  3. neumino commented on Jul 1, 2014

    @neumino
    Member

    If f can throw the same error, doesn't it mean that you need to catch the error earlier? (like inside f?)

  4. AtnNn commented on Jul 1, 2014

    @AtnNn
    MemberAuthor

    If f throws an error, it is not the caller's responsibility to handle it. The error should be passed on with an attached backtrace.

  5. mlucy commented on Jul 1, 2014

    @mlucy
    Member

    @AtnNn: We removed base because it was confusing people and causing them to write incorrect code. Maybe there's a way to get the behavior you want without re-introducing it? Or by calling something else (like an optarg if_empty: r.table('test')['a'].reduce(lambda a,b: a+b, if_empty=0))?

  6. added this to the subsequent milestone on Jul 1, 2014
  7. mlucy commented on Jul 1, 2014

    @mlucy
    Member

    Also, there's always r.table('test').union(['base']).reduce{|a,b| a+b}.

  8. Tryneus commented on Jul 1, 2014

    @Tryneus
    Member

    @AtnNn, regarding your initial query, why not put default on both a and b?

    >>> r.expr([{n:1},{}]).reduce(lambda a,b: a['n'].default(0) + b['n'].default(0))
    1
  9. AtnNn commented on Jul 1, 2014

    @AtnNn
    MemberAuthor

    @Tryneus: that was an example. In my actual code, I have a more complicated reduction function, and I want errors from it to be thrown because they are actual errors I would need to fix.

    @mlucy: I have been using .union([base]). Unfortunately it breaks on grouped streams. It turns them into grouped data.

    I like your if_empty idea a lot.

  10. mlucy commented on Jul 1, 2014

    @mlucy
    Member

    Cool. I put this in subsequent, since the 1.14 discussion period is already pretty big. if_empty sounds like a reasonable workaround to me.

  11. mlucy commented on Jul 1, 2014

    @mlucy
    Member

    Also. @AtnNn, if you really want to hack it:

    r([{n: 1}, {}]).reduce{|a,b| a['n']+b['n']}.default{|err|
      r.branch(err.eq("Cannot reduce over an empty stream."), 0, r.error)
    }.run
  12. mlucy commented on Jul 1, 2014

    @mlucy
    Member

    (The r.error with no argument means "rethrow the current error".)

  13. AtnNn commented on Jul 1, 2014

    @AtnNn
    MemberAuthor

    @mlucy: that doesn't work either. See #2629 (comment)

  14. mlucy commented on Jul 1, 2014

    @mlucy
    Member

    Wow, totally skimmed over that. Clearly you need to wrap f in a default that rethrows a different error message ^.^ .

  15. danielmewes commented on Dec 3, 2014

    @danielmewes
    Member

    @marshall007 also brought it up over here #3395 (comment)

    I think an if_empty opt arg would be really nice and easy to understand.

  16. marshall007 commented on Dec 3, 2014

    @marshall007
    Contributor

    @danielmewes I think the issue I brought up is slightly different. In my example, I'm not (necessarily) trying to specify a default for when the sequence is empty; rather, I want to specify the initial value for the left argument.

    sequence.reduce(function (left, right) { ... }, base)
    • If the sequence is empty and
      • ... base is provided, return base.
      • ... base is not provided, error.
    • If the sequence is not empty and
      • ... base is provided, the initial value of left will be base.
      • ... base is not provided, the initial value of left will be the first item in the sequence.
  17. mlucy commented on Dec 3, 2014

    @mlucy
    Member

    @marshall007 -- one of the reasons we removed the base argument is that it lead people to believe that reduce works like fold, which it doesn't. [1,2,3,4,5].fold(+) is guaranteed to become ((((1+2)+3)+4)+5), but [1,2,3,4,5].reduce(+) is free to become ((1+2)+3)+(4+5). We need to use the looser definition because when your reduce over a sharded table, we want to be able to process the shards in parallel and then combine the results at the end.

    Anyway, as a result, queries like table.reduce(lambda sum,row: sum + row['a'], base=0) will produce an error. You have to write table.map(lambda row: row['a']).reduce(lambda a,b: a+b) instead. We found that when we provided the base argument, people were very tempted to write the first sort of query, and had trouble understanding why it didn't work.

  18. marshall007 commented on Dec 4, 2014

    @marshall007
    Contributor

    @mlucy that makes sense. The two biggest use cases I run into all the time are (1) aggregating multiple values (should be less painful after #2078) and (2) merging objects into a single dictionary. My biggest gripe is with (2) because most of the time it's faster to pull all the rows down and do the reduction in code. I assume this is because we have to map over every row before starting the reduction.

    ## code: 183ms ##
    r.table 'standard_terms'
    .getAll 'disorders', index: 'field'
    .run().then (data) ->
      dict = data.reduce (right, left) ->
        right[left.term] = left
        return right
      , {}
    
    ## db: 595ms ##
    r.table 'standard_terms'
    .getAll 'disorders', index: 'field'
    .map (row) -> r.object row('term'), row
    .reduce (l, r) -> l.merge r
    .run()
  19. danielmewes commented on Dec 4, 2014

    @danielmewes
    Member

    @marshall007 It's interesting that you see this performance in (2). I opened #3408 so we can investigate this further.

  20. modified the milestones: , subsequent on Apr 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions