ReactiveCocoa (2) map,filter,fold
来源:互联网 发布:上海地区大学 知乎 编辑:程序博客网 时间:2024/06/07 09:12
关于 ReactiveCocoa,首先需要了解三个概念:map,filter 和 fold。
Functional Programming with RXCollections
One of the key concepts of functional programming is that of a “higher-order function”.
Accordingto Wikipedia, a higher-order function is a function that satisfies these two conditions:
• It takes one or more functions as input.
• It outputs a function.
In Objective-C, we often use blocks as functions.
Use CocoaPods to install RXCollections in your project. In your application:didFinishLaunchingWithOptions: method,do some code,create the array first.
NSArray *array =@[@(1),@(2),@(3)];
Map:遍历
map(1,2,3)=>(1,4,9)
using RXCollections:
//map NSArray *mappedArray = [array rx_mapWithBlock:^id(id each) { return @(pow([each integerValue], 2)); }]; NSLog(@"%@",mappedArray);
common:
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:array.count]; for (NSNumber *number in array) { [mutableArray addObject:@(pow([number integerValue], 2))]; } NSArray *mappedArray = [NSArray arrayWithArray:mutableArray];
Filter:过滤
Filtering a list just returns a new list containing all of the original entries,
minus the entries that didn’t return true from a test.
using RXCollections:
//filter NSArray *filteredArray = [array rx_filterWithBlock:^BOOL(id each) { return ([each integerValue]%2 == 0); }]; NSLog(@"%@",filteredArray);
common:
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:array.count]; for (NSNumber *number in array) { if ([number integerValue]%2 == 0) { [mutableArray addObject:number]; } } NSArray *filteredArray = [NSArray arrayWithArray:mutableArray];
Fold:组合
A simple fold can be used to combine the integer values of each member of our array to calculate their sum.
//fold NSNumber *sum = [array rx_foldWithBlock:^id(id memo, id each) { //memo : return value of the previous invocation of the block (the initial value is nil). return @([memo integerValue] + [each integerValue]); }]; NSLog(@"%@",sum);
Another test:
[[array rx_mapWithBlock:^id(id each) { return [each stringValue]; }] rx_foldInitialValue:@"" block:^id(id memo, id each) { return [memo stringByAppendingString:[each stringValue]]; }];you can NSLog,is @“123”.
Conclusion:
We also saw, in the last example, how we can chain operations together to get a more complex result.
In fact,chaining operations together is one of the main principles of using ReactiveCocoa.
- ReactiveCocoa (2) map,filter,fold
- Haskell语言学习笔记(3)map, filter, fold
- Fold
- fold
- FILTER&MAP
- ReactiveCocoa 2
- ReactiveCocoa 2
- Bit-map (2) Bloom Filter 和 Counting Bloom Filter
- python进阶2:map()、reduce()、filter()
- filter,map,reduce
- filter,map,reduce
- python:map filter reduce
- filter、map、reduce、lambda
- map,enumerate,zip,filter
- JavaScript "filter" and "map"
- python filter and map
- filter()map() reduce()
- python filter/map/reduce
- 设计模式笔记之工厂方法模式
- copy的
- C#,由委托到Observer设计模式,再到事件机制
- 崔永元:中国转基因滥种最厉害 两会上会出示证据
- POJ 2002 Squares 几何 + 二分搜索 或 hash
- ReactiveCocoa (2) map,filter,fold
- 729 - The Hamming Distance Problem(可重集排列,直接枚举)
- Hyperic-Sigar简介
- getchar如何输入输出三位数字问题
- iOS ViewController的生命周期及其加载View的步骤
- spring 事物学习
- 黑马程序员 Java基础学习笔记 线程间同信
- eclipse导入工程中文乱码问题
- HDU 3631 Shortest Path