Group by

See also

  • Official ReactiveX documentation: GroupBy
Observable.group_by(key_selector, element_selector=None, key_serializer=None)

Groups the elements of an observable sequence according to a specified key selector function and comparer and selects the resulting elements by using a specified function.

1 - observable.group_by(lambda x: x.id) 2 - observable.group_by(lambda x: x.id, lambda x: x.name) 3 - observable.group_by(

lambda x: x.id, lambda x: x.name, lambda x: str(x))

Keyword arguments: key_selector – A function to extract the key for each element. element_selector – [Optional] A function to map each source element to

an element in an observable group.
comparer – {Function} [Optional] Used to determine whether the objects
are equal.

Returns a sequence of observable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.

../../_images/groupBy.png
Observable.group_by_until(key_selector, element_selector, duration_selector, comparer=None)

Groups the elements of an observable sequence according to a specified key selector function. A duration selector function is used to control the lifetime of groups. When a group expires, it receives an OnCompleted notification. When a new element with the same key value as a reclaimed group occurs, the group will be reborn with a new lifetime request.

1 - observable.group_by_until(
lambda x: x.id, None, lambda : Rx.Observable.never()

)

2 - observable.group_by_until(
lambda x: x.id, lambda x: x.name, lambda: Rx.Observable.never()

)

3 - observable.group_by_until(
lambda x: x.id, lambda x: x.name, lambda: Rx.Observable.never(), lambda x: str(x))

Keyword arguments: key_selector – A function to extract the key for each element. duration_selector – A function to signal the expiration of a group. comparer – [Optional] {Function} Used to compare objects. When not

specified, the default comparer is used. Note: this argument will be ignored in the Python implementation of Rx. Python objects knows, or should know how to compare themselves.

Returns a sequence of observable groups, each of which corresponds to a unique key value, containing all elements that share that same key value. If a group’s lifetime expires, a new group with the same key value can be created once an element with such a key value is encountered.

../../_images/groupByUntil.png
Observable.partition(predicate)

Returns two observables which partition the observations of the source by the given function. The first will trigger observations for those values for which the predicate returns true. The second will trigger observations for those values where the predicate returns false. The predicate is executed once for each subscribed observer. Both also propagate all error observations arising from the source and each completes when the source completes.

Keyword arguments: predicate – The function to determine which output Observable will

trigger a particular observation.

Returns a list of observables. The first triggers when the predicate returns True, and the second triggers when the predicate returns False.