Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

FRP - Functional Reactive Programming

4,339 views

Published on

関数リアクティブプログラミングをAndroidやiOSで使ってみた話

Published in: Technology

FRP - Functional Reactive Programming

  1. 1. FRP Functional Reactive Programming 2014/08/01 Ryo Kamimura
  2. 2. いいか、みんな          (゚д゚ )          (| y |)   関数型プログラミングの要素をリアクティブプログラミングに合わせると     関数型 (゚д゚) リアクティブ       \/| y |\/       FRPになるのだ          ( ゚д゚) FRP          (\/\/
  3. 3. (reactive) ( ) ( ) (behavior)
  4. 4. (stateless) (immutable) (map, filter, fold)
  5. 5. Java Objective-C FRP RxJava compile 'com.netflix.rxjava:rxjava-core:+' ReactiveCocoa pod 'ReactiveCocoa'
  6. 6. Push Pull Push Pull
  7. 7. ( ) Pull java.lang.Iterable, RACSequence(ReactiveCocoa) Push rx.Observable(RxJava), RACSignal(ReactiveCocoa) Push
  8. 8. Push UI 3 next completed error
  9. 9. (Java) ViewObservable.input(mEditText, false) .subscribe(new Action1<String>() { @Override public void call(String s) { Log.d("LOG", "New value: " + s); } }, new Action1<Throwable>() { @Override public void call(Throwable t) { Log.d("LOG", "Error: " + t.getMessage()); } }, new Action0() { Override public void call() { Log.d("LOG", "Completed."); } });
  10. 10. (Objective-C) [self.textField.rac_textSignal subscribeNext:^(id x){ NSLog(@"New value: %@", x); } error:^(NSError *error){ NSLog(@"Error: %@", error); } completed:^{ NSLog(@"Completed."); }];
  11. 11. FRP KVO UI
  12. 12. Transformation Filtering Combining Injection
  13. 13. Transformation map() / map: flatMap() / flattenMap:
  14. 14. Filtering filter() / filter: skip() / skip: throttleWithTimeout() / throttle:
  15. 15. Combining merge() / merge: zip() / zip:reduce:
  16. 16. Injection doOnEach() / doNext: doOnCompleted() / doCompleted: doOnError() / doError:
  17. 17. FRP 1. 2. 3. 500 4. 5.
  18. 18. Java 8 ViewObservable.input(mEditText, false) .filter(text -> { // 入力値をチェックして return isValidText(text); }) .throttleWithTimeout(500, TimeUnit.MILLISECONDS) // 500ミリ秒以内 .flatMap(text -> { // 入力値をサーバーに送信 return sendTextObservable(text); }) .flatMap(jsonResponse -> { // レスポンスの値を使って再度サーバーからデータ取得 return resultObservable(jsonResponse.get("result")); }) .observeOn(AndroidSchedulers.mainThread()) // UIスレッドで .subscribe(jsonResponse -> { // UIを更新 }, throwable -> { Log.d("LOG", "An error occurred: " + throwable.getMessage()); });
  19. 19. Objective-C [[[[[[[self.textField rac_textSignal] filter:^BOOL(NSString *text) { // 入力値をチェックして @strongify(self) return [self isValidText:text]; }] throttle:0.5] // 500ミリ秒以内 flattenMap:^RACStream*(NSString *text) { // 入力値をサーバーに送信 @strongify(self) return [self signalForSendText:text]; }] flattenMap:^RACStream*(NSDictionary *jsonResponse) { // レスポンスの値を使って再度サーバーからデータ取得 @strongify(self) return [self signalForResult:jsonResponse[@"result"]] }] deliverOn:[RACScheduler mainThreadScheduler]] // UIスレッドで subscribeNext:^(NSDictionary *jsonRespoonse) { // UIを更新 } error:^(NSError *error) { NSLog(@"An error occurred: %@", error); }];
  20. 20. FRP GUI UI Push
  21. 21. GUI ON/OFF (Spinner/UIPickerView)
  22. 22. Non-FRP UI
  23. 23. FRP
  24. 24. iOS Android Java Java8 (+ Retrolambda) Objective-C Groovy Swift
  25. 25. THE END

×