Skip to content

scan command for operating on ordered sequences #3736

Closed

Description

@AtnNn

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:

sequence.scan(state, f, end) ≡
  case f(state, head(sequence)) of
    [state′, out] → out.union(tail(sequence).scan(state′, f, end))
    null → ∅
∅.scan(state, f, end) ≡
  end(state)

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:

table.order_by(index='time').scan({'val': null, 'group':[]}, lambda state, row:
  r.branch(state['val'] == row['time'].truncate('seconds'),
    [{'val': state['val'], 'group': state['group'].add([row])}, []],
    [{'val': row['time'].truncate('seconds'), 'group': [row]}), [state['group']]],
  lambda state: [state['group']])

To count added and deleted rows:

changefeed.scan(0, lambda n, change:
  r.branch(change['new_val'],1,0).add(
    r.branch(change['old_val'],-1,0)).do(
      lambda new_n: [new_n, [new_n]])))

Or to compute the Fibonacci sequence (compare to http://rethinkdb.com/blog/fib/):

r.range().scan(1, lambda a, b: [a + b, [a]])

With #3579 it would also open a lot of possibilities for operating on changefeeds.

I also wonder if better abstractions exists for operating on sequences in order and with state.

Activity

  1. added this to the subsequent milestone on Feb 5, 2015
  2. danielmewes commented on Feb 5, 2015

    @danielmewes
    Member

    This is kind of related to something that @wojons suggested just a few days ago #3724, though scan is a lot more general.

  3. coffeemug commented on Feb 6, 2015

    @coffeemug
    Contributor

    Note that this also might be related to #2664.

  4. modified the milestones: , subsequent on Apr 21, 2015
  5. 17 remaining items

  6. mlucy commented on Dec 15, 2015

    @mlucy
    Member

    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.

  7. danielmewes commented on Dec 16, 2015

    @danielmewes
    Member

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

  8. AtnNn commented on Dec 16, 2015

    @AtnNn
    MemberAuthor

    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 foldl in 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| ... }

  9. mlucy commented on Dec 16, 2015

    @mlucy
    Member

    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.

  10. AtnNn commented on Dec 16, 2015

    @AtnNn
    MemberAuthor

    @mlucy: I didn't seem as natural to me at first, but after thinking about it more I fully agree with you.

  11. danielmewes commented on Dec 17, 2015

    @danielmewes
    Member

    Marking settled with fold(base, function(acc, el)[, {emit: function(old_acc, el, new_acc)}, final_emit: function(acc)])

  12. self-assigned this
    on Jan 18, 2016
  13. nighelles commented on Jan 18, 2016

    @nighelles
  14. added a commit that references this issue on Feb 5, 2016
  15. nighelles commented on Feb 5, 2016

    @nighelles

    This is in next.

  16. danielmewes commented on Feb 5, 2016

    @danielmewes
    Member

    👏 Yay, have been waiting for this term for a long time :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions