ReactiveCocoa (2) map, filter, fold

Source: Internet
Author: User

For ReactiveCocoa, you must first understand three concepts: map, filter, and 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: Traversal

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: 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: combine 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, Which 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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.