Last active
January 7, 2016 10:08
-
-
Save Jim-Lin/223d9175a1fd3b78ff24 to your computer and use it in GitHub Desktop.
[ReactiveX Groovy] map & flatMap & concatMap sample ref: http://qiita.com/SHUAI/items/4b7d29b47919900d01bf
This file contains 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
numbers = Observable.from([1, 2, 3, 4, 5]); | |
numbers.map({it * it}).subscribe( | |
{ println(it); }, // onNext | |
{ println("Error: " + it.getMessage()); }, // onError | |
{ println("Sequence complete"); } // onCompleted | |
); |
This file contains 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
1 | |
4 | |
9 | |
16 | |
25 | |
Sequence complete |
This file contains 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
// this closure is an Observable that emits three numbers | |
numbers = Observable.from([1, 2, 3]); | |
// this closure is an Observable that emits two numbers based on what number it is passed | |
multiples = { n -> Observable.from([ n*2, n*3 ]) }; | |
numbers.flatMap(multiples).subscribe( | |
{ println(it); }, // onNext | |
{ println("Error: " + it.getMessage()); }, // onError | |
{ println("Sequence complete"); } // onCompleted | |
); |
This file contains 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
2 | |
3 | |
4 | |
6 | |
6 | |
9 | |
Sequence complete |
This file contains 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
List<Integer> numbers; | |
Executor jobExecutor; | |
DataManager() { | |
this.numbers = new ArrayList<>(Arrays.asList(2, 3, 4, 5, 6, 7, 8, 9, 10)); | |
this.jobExecutor = JobExecutor.getInstance(); | |
} | |
Observable<Integer> squareOfAsync(int number) { | |
return Observable.just(number * number).subscribeOn(Schedulers.from(this.jobExecutor)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment