0

I'm using group_by on an Observable but for each newly created group, I want to snap the element (with the new key) that caused the group to be created using with_latest_from:

>>> from __future__ import print_function
>>> from rx import Observable

>>> # sequence 1, 2, 3, ... every half a second
>>> observable=Observable.interval(500).map(lambda x: x + 1)

>>> # groups into numbers that are divisible by 3 (True) and those that are not (False)
>>> grouped = observable.group_by(lambda x: bool(x%3))

>>> # groups paired with the first element that kicked off the group
>>> grouped.with_latest_from(observable, lambda group, element: (group, element)).subscribe(print)

I'm expecting to see both of the below to be printed, but am only seeing either one each time.

(<rx.linq.groupedobservable.GroupedObservable object at 0xabc>, 1)  # 1 is the element that created group with key=False
(<rx.linq.groupedobservable.GroupedObservable object at 0xdef>, 3)  # 3 is the element that created group with key=True

On the odd occasion I also see the snapped element as 2:

(<rx.linq.groupedobservable.GroupedObservable object at 0x0313EB10>, 2)

Any ideas what is going wrong?

mchen
  • 9,808
  • 17
  • 72
  • 125

1 Answers1

0

From Dag Brattli:

The problem seems to be that you are basically using with_latest_from() on a stream with itself. Thus there are no guaranties if source or latest will trigger first. If latest triggers before source for a new group, then it will be lost. A way to solve it is to get a separate stream of the first elements in each group and zip that with the stream of groups:

# A stream with the first element if each group
firsts = grouped.flat_map(lambda group: group.first())

# groups paired with the first element that kicked off the group
grouped.zip(firsts, lambda g, k: (g, k)).subscribe(print)

Also note that each group also contains the key that is the result of the modulus operator if that is something that can be used instead of the element:

grouped.map(lambda gr: (gr, gr.key)).subscribe(print)
mchen
  • 9,808
  • 17
  • 72
  • 125