本文内容仅适用于ReactiveCocoa v2.5
关于函数响应式编程(FRP),可以参考
- What is (functional) reactive programming?
- Specification for a Functional Reactive Programming language
Streams of values over time
ReactiveCocoa repo上最简单的一句话对FRP做了本质的描述,而repo本身提供的是APIs of composing and transforming streams of values,而Streams of values的抽象在ReactiveCocoa中应该是RACStream,而composing and transforming是本文的重点。
先解释ReactiveCocoa中的两个基本概念
信号(Signal) a signal is a steam of values,signals can be transformed, combined,etc.
订阅者(Subscriber) a subscriber subscribes to a signal. RAC lets blocks,objects, and properties subscribe to signals
filter
RACSignal *signal = [@[ @1, @2, @3 ] rac_sequence].signal; signal = [signal filter:^BOOL(NSNumber *value) {
return value.integerValue % 2;
}];
[signal subscribeNext:^(NSNumber *value) {
NSLog(@"%@", value);
}];
map
RACSignal *signal = [@[ @1, @2, @3 ] rac_sequence].signal;
signal = [signal map:^id(NSNumber *value) {
return @(value.integerValue * 2);
}];
[signal subscribeNext:^(NSNumber *value) {
NSLog(@"%@", value);
}];
merge
RACSignal *signal1 = [@[ @1, @2 ] rac_sequence].signal;
RACSignal *signal2 = [@[ @4, @5 ] rac_sequence].signal;
[[signal1 merge:signal2] subscribeNext:^(NSNumber *value) {
NSLog(@"%@", value);
}];
combineLatest
RACSignal *signal1 = [@[ @1, @2 ] rac_sequence].signal;
RACSignal *signal2 = [@[ @3, @4 ] rac_sequence].signal;
[[signal1 combineLatestWith:signal2] subscribeNext:^(RACTuple *value) {
NSLog(@"%@", value);
}];
combineLatest & reduce
RACSignal *signal1 = [@[ @1, @2 ] rac_sequence].signal;
RACSignal *signal2 = [@[ @3, @4 ] rac_sequence].signal;
[[[signal1 combineLatestWith:signal2]
reduceEach:^id(NSNumber *v1, NSNumber *v2) {
return @(v1.integerValue * v2.integerValue);
}] subscribeNext:^(RACTuple *value) {
NSLog(@"%@", value);
}];
flatten
RACSignal *signal1 = [@[ @1, @2 ] rac_sequence].signal;
RACSignal *signal2 = [RACSignal return:signal1];
[[signal2 flatten] subscribeNext:^(NSNumber *value) {
NSLog(@"%@", value);
}];
flattenMap
RACSignal *signal = [@[ @1, @2 ] rac_sequence].signal;
[[signal flattenMap:^RACStream *(NSNumber *value) {
return [RACSignal return:@(value.integerValue * 2)];
}] subscribeNext:^(NSNumber *value) {
NSLog(@"%@", value);
}];
not replay
RACSubject *letters = [RACSubject subject];
RACSignal *signal = letters;
[signal subscribeNext:^(id x) {
NSLog(@"S1: %@", x);
}];
[letters sendNext:@"A"];
[signal subscribeNext:^(id x) {
NSLog(@"S2: %@", x);
}];
[letters sendNext:@"B"];
[letters sendNext:@"C"];
replay
RACSubject *letters = [RACReplaySubject subject];
RACSignal *signal = letters;
[signal subscribeNext:^(id x) {
NSLog(@"S1: %@", x);
}];
[letters sendNext:@"A"];
[signal subscribeNext:^(id x) {
NSLog(@"S2: %@", x);
}];
[letters sendNext:@"B"];
[signal subscribeNext:^(id x) {
NSLog(@"S3: %@", x);
}];
[letters sendNext:@"C"];
replayLast
RACSubject *letters = [RACSubject subject];
RACSignal *signal = [letters replayLast];
[signal subscribeNext:^(id x) {
NSLog(@"S1: %@", x);
}];
[letters sendNext:@"A"];
[signal subscribeNext:^(id x) {
NSLog(@"S2: %@", x);
}];
[letters sendNext:@"B"];
[signal subscribeNext:^(id x) {
NSLog(@"S3: %@", x);
}];
[letters sendNext:@"C"];
replayLazily
RACSubject *letters = [RACSubject subject];
RACSignal *signal = [letters replayLazily];
[letters sendNext:@"A"];
[signal subscribeNext:^(id x) {
NSLog(@"S1: %@", x);
}];
[letters sendNext:@"B"];
[signal subscribeNext:^(id x) {
NSLog(@"S2: %@", x);
}];
[letters sendNext:@"C"];
[signal subscribeNext:^(id x) {
NSLog(@"S3: %@", x);
}];
[letters sendNext:@"D"];
zip
RACSubject *letters = [RACSubject subject];
RACSubject *numbers = [RACSubject subject];
RACSignal *combined =
[RACSignal zip:@[ letters, numbers ]
reduce:^(NSString *letter, NSString *number) {
return [letter stringByAppendingString:number];
}];
// Outputs: A1 B2 C3
[combined subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
[letters sendNext:@"A"];
[letters sendNext:@"B"];
[letters sendNext:@"C"];
[numbers sendNext:@"1"];
[numbers sendNext:@"2"];
[numbers sendNext:@"3"];
完整的源码可以在这里找到 Demo