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
{{ message }}
Repository navigation
scan command for operating on ordered sequences #3736
I have had in mind a command that could be used to implement many operations on sequences that are not currently possible. It would behave a little like concat_map, but with state. In pseudo-code, it would be something like this:
The function f would be given the current state and the next input row, and it would produce the next state, and possibly generate output rows. f could also return null to end the sequence. The optional third argument end would be called when the input sequence is exhausted.
For example, it could be used to return every third row:
sequence.scan(0, lambda n, row: [n+1, r.branch(n.mod(3) == 0, [row], [])])
To group rows without loading the whole table in memory:
If we make it positional it would be harder to make it optional later, but yeah, I think positional argument is better. I would have a moderately strong preference for putting base before f though.
I think fold(base, f) might read better, because you have all the data to the left, and the function body to the right (data.fold(data, function {.......})), rather than having data.fold(function {...........}, data).
I'm happy with either option. Let's go with fold(base, f[, {emit: emitFunc}]) unless someone has strong other opinions.
If you handed me a new language with a function called fold or reduce, I would assume the accumulator is on the left, personally. I agree that it's a little weird because we but the base after the sequence, but I still think the accumulator being the first argument seems more natural.
Activity
danielmewes commentedon Feb 5, 2015
This is kind of related to something that @wojons suggested just a few days ago #3724, though
scanis a lot more general.coffeemug commentedon Feb 6, 2015
Note that this also might be related to #2664.
neumino commentedon Feb 8, 2015
It would help for things like this too:
http://stackoverflow.com/questions/28371303/funnels-and-rethinkdb
17 remaining items
mlucy commentedon Dec 15, 2015
If we make it positional it would be harder to make it optional later, but yeah, I think positional argument is better. I would have a moderately strong preference for putting
basebeforefthough.danielmewes commentedon Dec 16, 2015
Looking at https://en.wikipedia.org/wiki/Fold_%28higher-order_function%29#Folds_in_various_languages, about half of the languages seem to put the base first and half the function. Most functional languages (but also JavaScript and Python) seem to take the function first, while some traditionally procedural languages (C#, C++, D, Java) have the base first.
I think
fold(base, f)might read better, because you have all the data to the left, and the function body to the right (data.fold(data, function {.......})), rather than havingdata.fold(function {...........}, data).I'm happy with either option. Let's go with
fold(base, f[, {emit: emitFunc}])unless someone has strong other opinions.AtnNn commentedon Dec 16, 2015
I think having the base first and function second would be nicer too. What about the order of the arguments to the function?
The type of
foldlin Haskell places the accumulator first, which produce a nice symmetry in the order or arguments:(b -> a -> b) -> (b -> [a] -> b).To get a similar symmetry in ReQL, the order of the arguments would have to be like in Erlang:
els.fold(acc0){ |el acc| ... }mlucy commentedon Dec 16, 2015
If you handed me a new language with a function called
foldorreduce, I would assume the accumulator is on the left, personally. I agree that it's a little weird because we but the base after the sequence, but I still think the accumulator being the first argument seems more natural.AtnNn commentedon Dec 16, 2015
@mlucy: I didn't seem as natural to me at first, but after thinking about it more I fully agree with you.
danielmewes commentedon Dec 17, 2015
Marking settled with
fold(base, function(acc, el)[, {emit: function(old_acc, el, new_acc)}, final_emit: function(acc)])nighelles commentedon Jan 18, 2016
This is in review 3431
Implemented in branch nighelles_3736
https://github.com/rethinkdb/rethinkdb/tree/nighelles_3736
Implementing fold term for #3736
nighelles commentedon Feb 5, 2016
This is in next.
danielmewes commentedon Feb 5, 2016
👏 Yay, have been waiting for this term for a long time :)
Implementing fold term for rethinkdb#3736