Skip to content

Instantly share code, notes, and snippets.

@benjchristensen
Last active December 11, 2015 23:28
Show Gist options
  • Save benjchristensen/4676544 to your computer and use it in GitHub Desktop.
Save benjchristensen/4676544 to your computer and use it in GitHub Desktop.
Simple example of applying Rx operators to an asynchronous Observable sequence.
/**
* Asynchronously calls 'customObservableNonBlocking' and defines
* a chain of operators to apply to the callback sequence.
*/
def simpleComposition() {
// fetch an asynchronous Observable<String>
// that emits 75 Strings of 'anotherValue_#'
customObservableNonBlocking()
// skip the first 10
.skip(10)
// take the next 5
.take(5)
// transform each String with the provided function
.map({ stringValue -> return stringValue + "_transformed"})
// subscribe to the sequence and print each transformed String
.subscribe({ println "onNext => " + it})
}
// output
onNext => anotherValue_10_transformed
onNext => anotherValue_11_transformed
onNext => anotherValue_12_transformed
onNext => anotherValue_13_transformed
onNext => anotherValue_14_transformed
@felixbarny
Copy link

line 14 could be simplified to

.map({ "$it_transformed" })

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