# 图解ReactiveCocoa基本函数

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是本文的重点。

• 信号（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);
}];
``````
filter

## 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);
}];
``````
map

## merge

``````RACSignal *signal1 = [@[ @1, @2 ] rac_sequence].signal;
RACSignal *signal2 = [@[ @4, @5 ] rac_sequence].signal;

[[signal1 merge:signal2] subscribeNext:^(NSNumber *value) {
NSLog(@"%@", value);
}];
``````
merge

## combineLatest

``````  RACSignal *signal1 = [@[ @1, @2 ] rac_sequence].signal;
RACSignal *signal2 = [@[ @3, @4 ] rac_sequence].signal;

[[signal1 combineLatestWith:signal2] subscribeNext:^(RACTuple *value) {
NSLog(@"%@", value);
}];
``````
combineLatest

## 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);
}];
``````
combineLatest & reduce

## flatten

``````RACSignal *signal1 = [@[ @1, @2 ] rac_sequence].signal;
RACSignal *signal2 = [RACSignal return:signal1];

[[signal2 flatten] subscribeNext:^(NSNumber *value) {
NSLog(@"%@", value);
}];
``````
flatten

## flattenMap

``````RACSignal *signal = [@[ @1, @2 ] rac_sequence].signal;

[[signal flattenMap:^RACStream *(NSNumber *value) {
return [RACSignal return:@(value.integerValue * 2)];
}] subscribeNext:^(NSNumber *value) {
NSLog(@"%@", value);
}];
``````
flattenMap

## 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"];
``````
not replay

## 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"];
``````
replay

## 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"];
``````
replayLast

## 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"];
``````
replayLazily

## 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"];
``````
zip