内容简介:标签(空格分隔): 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树
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
高性能JavaScript
Nicholas C.Zakas / 丁琛、赵泽欣 / 电子工业出版社 / 2010-11 / 49.00元
如果你使用JavaScript构建交互丰富的Web应用,那么JavaScript代码可能是造成你的Web应用速度变慢的主要原因。《高性能JavaScript》揭示的技术和策略能帮助你在开发过程中消除性能瓶颈。你将会了解如何提升各方面的性能,包括代码的加载、运行、DOM交互、页面生存周期等。雅虎的前端工程师Nicholas C. Zakas和其他五位JavaScript专家介绍了页面代码加载的最佳方......一起来看看 《高性能JavaScript》 这本书的介绍吧!