Skip to content

Instantly share code, notes, and snippets.

@Pamplemousse
Created December 13, 2016 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pamplemousse/dc77fecfe7fdbe8875f439b847f9dc62 to your computer and use it in GitHub Desktop.
Save Pamplemousse/dc77fecfe7fdbe8875f439b847f9dc62 to your computer and use it in GitHub Desktop.
pair_with_previous method for Observable instances in RxPy
from rx.core import Observable, AnonymousObservable
from rx.internal import extensionmethod
@extensionmethod(Observable)
def pair_with_previous(self):
"""
stream = Observable.from_iterable(
list(map(
lambda x: [1*x, 2*x],
range(6)))
)
stream = stream.pair_with_previous()
stream.subscribe(print)
"""
return self.scan(
lambda acc, current: (acc[1], current),
(None, None)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment