ReactiveCocoa (2) map,filter,fold

本文深入介绍了ReactiveCocoa中map、filter和fold的概念及使用,通过Objective-C示例展示了如何在RXCollections中进行函数式编程,包括用map进行遍历转换,filter进行数据过滤,以及fold用于数值组合计算,揭示了ReactiveCocoa中操作链式调用的核心思想。
摘要由CSDN通过智能技术生成

关于 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.

In fact,chaining operations together is one of the main principles of using ReactiveCocoa.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值