RxJava Nightã«åãã¦RxJavaã«ã¤ãã¦èª¿ã¹å§ãããã®ã®ãæ¥æ¬èªã®æ
å ±ãã»ã¨ãã©ç¡ãã¦è¾ãã£ãã®ã§èª¿ã¹ãäºãã¾ã¨ãã¾ããã
RxJavaããªã¢ã¯ãã£ãããã°ã©ãã³ã°ã«ã¤ãã¦ã¯ä»ã¾ã§ã¾ã£ããç¥ããªãã£ãã®ã§ãå
容ã«ã¯ééããå«ã¾ãã¦ããå¯è½æ§ãããã¾ããäºãã容赦é ããä¸ã§ã覧ãã ããã
å
ãã¿ã¯ã»ã¼
ReactiveX/RxJava/Wiki
Grokking RxJava
【翻訳】あなたが求めていたリアクティブプログラミング入門 - ninjinkun's diary
ã§ãã
RxJavaã¨ã¯
RxJavaã¨ã¯ãªã¢ã¯ãã£ãããã°ã©ãã³ã°ãè¡ãããã®ã©ã¤ãã©ãªã§ããRx(Reactive Extensions)ã®JVMçã¨ã®ãã¨ã
ãªã¢ã¯ãã£ãããã°ã©ãã³ã°ã®æ¦å¿µã«ã¤ãã¦ã¯ããã»ã©ããç解åºæ¥ã¦ããªãã®ã§ãä¸è¨ã®è¨äºãªã©ãåç
§ãã¦ãã ããã
StreamAPIï¼éåæã³ã¼ã«ããã¯ã¿ãããªæãã
ã¤ãã³ããå®ç¾©ããObservableã¤ã³ã¹ã¿ã³ã¹ã«å¯¾ãã¦çãé¢æ°ããã§ã¼ã³ãã¦ããã¹ããªã¼ã çã«ã¤ãã³ãçµæã®ãã¼ã¿ãå å·¥ããå¦çãå®ç¾©ããé
延å®è¡ã»éåæã³ã¼ã«ããã¯ãè¡ãäºãåºæ¥ã¾ãã
åºæ¬çãªä½¿ãæ¹
å¦çãå®è¡ãã¦çµæãä¼ããObservableã¨ãçµæãåãåã£ãæã®å¦çã決ããObserverã使ãã
ã¾ãã¯"Hello"ã¨"world"ã¨ãã2ã¤ã®æååãçµæã¨ãã¦ä¼ããã·ã³ãã«ãªObservableçæãã¦ã¿ãã
â»java.util.Observableã§ã¯ãªãã®ã§æ³¨æ
Observable<String> myObservable = Observable.create( new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { subscriber.onNext("Hello"); subscriber.onNext("world!"); subscriber.onCompleted(); } } );
次ã«çµæãåãåãå´ã§ããObserverãçæããã
Observer<String> myObserver = new Observer<String>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(String s) { System.out.println(s); } };
æå¾ã«Observableã®subscribeã¡ã½ããã«Observerã渡ãã¦å®è¡ã
ãã®æç¹ã§åãã¦Observableãå®è¡ãããonNextã«æ¸¡ããçµæãé çªã«Observerã«ä¼ããã
myObservable.subscribe(myObserver);
ããã¦ã
"Hello"
"world!"
ãåºåãããã
ãã£ã¨ç°¡æ½ã«æ¸ã
ä¸è¨ã¯ä¸å¯§ãªä¾ã ããç¨æãããæ§ã
ãªã¡ã½ãããæ´»ç¨ããäºã§ãã£ã¨ç°¡æ½ã«è¨è¿°ããäºãåºæ¥ãã
fromã¯åãåã£ãé
åãIteratorã®è¦ç´ ãé çªã«çµæã¨ãã¦è¿ãObservableãçæãã¦ãããã
ã¾ããsubscribeã«ã¯Observerã®ä»£ããã«é¢æ°(Action1ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®å®è£
)ãä¸ã¤ã ã渡ãäºã§ãonNextæã«é¢æ°ãå®è¡ãã¦ãããã
Observable<String> myObservable = Observable.from(new String[]{"Hello", "world!"}); myObservable.subscribe(new Action1<String>() { @Override public void call(String s) { System.out.println(s); } });
Java8ãªãAction1ã¯é¢æ°åã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ãªãã®ã§ããã¾ã§ã®å¦çã¯ã©ã ãå¼ã§ä¸è¡ã«ãªãã
Observable.from(new String[]{"Hello", "world!"}).subscribe(System.out::println);
Observableãæ°ããªObservableã«å¤æãã
map
mapã¡ã½ããã¯Observableãå¥ã®Observableã«å¤æããã
ä¾ãã°å
ç¨ã®Observableããåãåãããã®ãæååãã®ãã®ã§ã¯ãªãã¦æååã®é·ãã ã£ãã¨ããã
mapã使ãã°æååãä¼ããObservableãå
ã«æååã®é·ããä¼ããæ°ããªObservableãç°¡åã«çæããäºãåºæ¥ãã
Observable.from(new String[]{"Hello", "world!"}) .map(new Func1<String, Integer>() { @Override public Integer call(String s) { return s.length(); } }) .subscribe(new Action1<Integer>() { @Override public void call(Integer i) { System.out.println(i) } });
ããã¯
"5"
"6"
ãåºåããã
flatMap
ããã§ã¯é
åã®è¦ç´ ããã¹ããã¦ãããã©ãã ããã
flatMapã使ã£ã¦ãã¹ãããè¦ç´ ã並åã®çµæã¨ãã¦ä¼ããäºãåºæ¥ãã
ããã§flatMapã®Func1ã§ã¯Observableãè¿ãã¦ããããflatMapã¯ãã®Observableãå解ãã¦ä¸¦åãªä¸ã¤ã®Observableãçæãã¦ããã¦ããã
String[][] helloAndGoodbye = {{"Hello", "world!"}, {"goodbye", "world!"}}; Observable.from(helloAndGoodbye) .flatMap(new Func1<String[], Observable<String>>() { @Override public Observable<String> call(String[] strings) { // ããã«ä¼ããã®ã¯2ã¤ã®é åããããObservableã«ãã¦è¿ãã return Observable.from(strings); } }) .map(new Func1<String, Integer>() { @Override public Integer call(String s) { // ããã«ä¼ããã®ã¯4ã¤ã®æååã return s.length(); } }) .subscribe(new Action1<Integer>() { @Override public void call(Integer i) { System.out.println(i) } });
"5"
"6"
"7"
"6"
ãåºåãããã
marge
è¤æ°ã®Observableãåæããmargeã使ç¨ãã¦flatMapã使ããã«æ¸ããã¨ãåºæ¥ãã
String[][] helloAndGoodbye = {{"Hello", "world!"}, {"goodbye", "world!"}}; Observable.merge(Observable.from(helloAndGoodbye[0]), Observable.from(helloAndGoodbye[1])) .map(new Func1<String, Integer>() { @Override public Integer call(String s) { return s.length(); } }) .subscribe(new Action1<Integer>() { @Override public void call(Integer i) { System.out.println(i) } });
filter
filterã使ãã¨çµæãåæ¨é¸æããäºãåºæ¥ãã
ããã§ã¯æååã"world!"ã¨ä¸è´ãããã®ãé¤å¤ãã¦ããã
String[][] helloAndGoodbye = {{"Hello", "world!"}, {"goodbye", "world!"}}; Observable.from(helloAndGoodbye) .flatMap(new Func1<String[], Observable<String>>() { @Override public Observable<String> call(String[] strings) { return Observable.from(strings); } }) .filter(new Func1<String, Boolean>() { @Override public Boolean call(String s) { return !s.equals("world!"); } }) .map(new Func1<String, Integer>() { @Override public Integer call(String s) { return s.length(); } }) .subscribe(new Action1<Integer>() { @Override public void call(Integer i) { System.out.println(i) } });
"5"
"7"
ãåºåãããã
ããã¾ã§ãJava8ã®ã©ã ãå¼ã§æ¸ãã¨ä¸è¨ã®ããã«ãªãã
String[][] helloAndGoodbye = {{"Hello", "world!"}, {"goodbye", "world!"}}; Observable.from(helloAndGoodbye) .flatMap(Observable::from) .filter(s -> !s.equals("world!")) .map(String::length) .subscribe(System.out::println);
ç¡åã¯ã©ã¹ãé£ãªã£ã¦ããã¨æ£ç´ããã¾ãè¦ãããã³ã¼ãã§ã¯ãªãã§ãããã©ã ãå¼ã«ããã¨ifãforãå¤ç¨ãã¦å¦çãè¨è¿°ããã®ã«æ¯ã¹ããªãç´æçãªã³ã¼ãã«ãªãã¾ããã
Wikiãåç
§ãã¦è²°ãã°åãããã¨æãã¾ãããRxJavaã«ã¯ä»ã«ãç¸å½æ°ã®ä¾¿å©ãªé¢æ°ãç¨æããã¦ããããã§ãã
ä¾ã®ãããªæ¯è¼çåç´ãªå¦çã§ã¯ããã¾ã§æãé£ã¿ãæããªãããããã¾ãããããã¼ã¿ã®å å·¥ãè¤éã«ãªãã°ãªãã»ã©å¹æãçºæ®ããããªæ°ããã¾ãã
ã¾ããRxJavaã§å¦çãæ¸ãã¨èªç¶ã¨å¦çã®åä½ãå°ãããªãå
¥åã»åºåãæ確ã«ãªããããã¡ã½ããã¨ãã¦åãåºãã¦ãã¹ãæ¸ãã®ãããããããªãã¨ããã¡ãªãããããããã§ãã
RXJavaã«ã¯Androidç¨ã®ã¢ã¸ã¥ã¼ã«ãç¨æããã¦ããã®ã§ãå®éã®Androidã¢ããªã§ã®ã¦ã¼ã±ã¼ã¹ã«åãããä¾ãæ¸ãã¦ã¿ããã¨æãã¾ãã