内容简介:标签(空格分隔): RAC FRP 函数式编程 响应式编程本文内容仅适用于关于函数响应式编程(FRP),可以参考
标签(空格分隔): RAC FRP 函数式编程 响应式编程
本文内容仅适用于 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
"-g"选项 : 查看日志缓冲区信息; "-b"选项 : 加载一个日志缓冲区, 默认是 main, 下面详解; "-B"选项 : 以二进制形式输出日志;
如有任何知识产权、版权问题或理论错误,还请指正。
转载请注明原作者及以上信息。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 图解利用虚函数过GS保护
- 【5】JavaScript 函数高级——原型与原型链深入理解(图解)
- 图解集合 3 : CopyOnWriteArrayList
- 图解集合 4 :HashMap
- 图解集合 2 :LinkedList
- 图解B树
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
深入浅出HTML5编程
弗里曼 (Eric Friiman)、罗布森 (Elisabdth Robson) / 东南大学出版社 / 2012-4 / 98.00元
《深入浅出HTML5编程(影印版)(英文)》就是你的特快车票,它可以带你学习如何使用今天的标准同时也会是明日的最佳实践来搭建Web应用。同时,你会了解HTML5的新API的基本知识,甚至你还会弄明白这些API是如何与你的网页进行交互,JaVaScript如何为它们提供动力,以及你如何使用它们来搭建能够打动你的老板并且吸引你的朋友的Web应用。一起来看看 《深入浅出HTML5编程》 这本书的介绍吧!