Last active
August 29, 2015 14:20
-
-
Save fudini/7bdd91b7adc4ec141c29 to your computer and use it in GitHub Desktop.
GroupBy -> transform -> merge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var groupTransform = (stream$, f, keySelector, selector) => { | |
return Rx.Observable.create(observer => { | |
var disposable = new Rx.CompositeDisposable() | |
disposable.add(stream$.groupBy(keySelector, selector) | |
.subscribe(groupObservable => { | |
disposable.add(f(groupObservable, groupObservable.key).subscribe(observer)) | |
})) | |
return disposable | |
}) | |
} | |
var input$ = Rx.Observable.fromArray([ | |
{ | |
key: 1, | |
value: 10 | |
}, { | |
key: 2, | |
value: 20 | |
}, { | |
key: 2, | |
value: 200 | |
}, { | |
key: 1, | |
value: 10 | |
}, { | |
key: 3, | |
value: 3 | |
} | |
]) | |
var comp$ = groupTransform( | |
input$, | |
(s$, key) => s$.map(v => v * 2), | |
x => x.key, | |
x => x.value | |
) | |
var d = comp$.subscribe(x => console.log(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment