ReactiveCocoa (2) map,filter,fold

来源:未知 责任编辑:责任编辑 发表时间:2014-03-23 22:26 点击:

关于 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:组合

it combines each entry in a list down to a single value. For this reason, it’s often referred to as “combine”.

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.

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • cocos2d-x学习笔记(19)--label 、label atlas
  • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
  • Cocos2d-x学习(一):HelloWorld
  • cocos2dx在xcode下开发,编译到android上(2)
  • cocos2d 设置屏幕默认方向
  • Cocos2d-x 2.0 之 Actions “三板斧” 之一
  • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
  • cocos2d-x学习笔记(18)--游戏打包(windows平台)
  • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1