際際滷

際際滷Share a Scribd company logo
FRP
Functional Reactive Programming
2014/08/01
Ryo Kamimura
いいか?みんな
         (?д? )
         (| y |)
  v方侏プログラミングの勣殆をリアクティブプログラミングに栽わせると
    v方侏 (?д?) リアクティブ
      棕| y |棕
      FRPになるのだ
         ( ?д?) FRP
         (棕棕
(reactive)
( )
( ) (behavior)
(stateless)
(immutable)
(map, filter, fold)
Java Objective-C
FRP
RxJava
compile 'com.netflix.rxjava:rxjava-core:+'
ReactiveCocoa
pod 'ReactiveCocoa'
Push Pull
Push
Pull
( )
Pull
java.lang.Iterable, RACSequence(ReactiveCocoa)
Push
rx.Observable(RxJava), RACSignal(ReactiveCocoa)
Push
Push
UI
3
next
completed
error
FRP - Functional Reactive Programming
(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.");
}
});
(Objective-C)
[self.textField.rac_textSignal subscribeNext:^(id x){
NSLog(@"New value: %@", x);
} error:^(NSError *error){
NSLog(@"Error: %@", error);
} completed:^{
NSLog(@"Completed.");
}];
FRP
KVO
UI
Transformation
Filtering
Combining
Injection
Transformation
map() / map:
flatMap() / flattenMap:
Filtering
filter() / filter:
skip() / skip:
throttleWithTimeout() / throttle:
Combining
merge() / merge:
zip() / zip:reduce:
Injection
doOnEach() / doNext:
doOnCompleted() / doCompleted:
doOnError() / doError:
FRP
1.
2.
3. 500
4.
5.
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());
});
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);
}];
FRP
GUI
UI
Push
GUI
ON/OFF
(Spinner/UIPickerView)
FRP - Functional Reactive Programming
Non-FRP
UI
FRP
iOS Android
Java Java8 (+ Retrolambda)
Objective-C
Groovy Swift
THE END

More Related Content

FRP - Functional Reactive Programming