You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@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))?
@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.
@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.
@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.
@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.
Activity
neumino commentedon Jul 1, 2014
Shouldn't we have a
catchmethod instead?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
AtnNn commentedon Jul 1, 2014
@neumino That still catches too many errors. You can currently do:
But if the sequence is not empty and the call to
fthrows that same error, it is going to be caught by the handler.neumino commentedon Jul 1, 2014
If
fcan throw the same error, doesn't it mean that you need to catch the error earlier? (like insidef?)AtnNn commentedon Jul 1, 2014
If
fthrows an error, it is not the caller's responsibility to handle it. The error should be passed on with an attached backtrace.mlucy commentedon Jul 1, 2014
@AtnNn: We removed
basebecause 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 optargif_empty:r.table('test')['a'].reduce(lambda a,b: a+b, if_empty=0))?mlucy commentedon Jul 1, 2014
Also, there's always
r.table('test').union(['base']).reduce{|a,b| a+b}.Tryneus commentedon Jul 1, 2014
@AtnNn, regarding your initial query, why not put
defaulton bothaandb?AtnNn commentedon Jul 1, 2014
@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_emptyidea a lot.mlucy commentedon Jul 1, 2014
Cool. I put this in subsequent, since the 1.14 discussion period is already pretty big.
if_emptysounds like a reasonable workaround to me.mlucy commentedon Jul 1, 2014
Also. @AtnNn, if you really want to hack it:
mlucy commentedon Jul 1, 2014
(The
r.errorwith no argument means "rethrow the current error".)AtnNn commentedon Jul 1, 2014
@mlucy: that doesn't work either. See #2629 (comment)
mlucy commentedon Jul 1, 2014
Wow, totally skimmed over that. Clearly you need to wrap
fin adefaultthat rethrows a different error message ^.^ .danielmewes commentedon Dec 3, 2014
@marshall007 also brought it up over here #3395 (comment)
I think an
if_emptyopt arg would be really nice and easy to understand.marshall007 commentedon Dec 3, 2014
@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
leftargument.baseis provided, returnbase.baseis not provided, error.baseis provided, the initial value ofleftwill bebase.baseis not provided, the initial value ofleftwill be the first item in the sequence.mlucy commentedon Dec 3, 2014
@marshall007 -- one of the reasons we removed the
baseargument is that it lead people to believe thatreduceworks likefold, 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 writetable.map(lambda row: row['a']).reduce(lambda a,b: a+b)instead. We found that when we provided thebaseargument, people were very tempted to write the first sort of query, and had trouble understanding why it didn't work.marshall007 commentedon Dec 4, 2014
@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.
danielmewes commentedon Dec 4, 2014
@marshall007 It's interesting that you see this performance in (2). I opened #3408 so we can investigate this further.