Functional operators for Objective-C
Objective-C Python Swift Ruby
Switch branches/tags
Nothing to show
Latest commit fbc69db Feb 4, 2018

README.md

Apache License Travis

Functional operators for Objective-C

An Objective-C library of functional operators, derived from Swift.Sequence, that help you write more concise and readable code for collection transformations. Foundation collections supported include: NSArray, NSDictionary, NSOrderedSet, and NSSet.

Functional Operators

Filter

Loops over a collection and returns an array that contains elements that meet a condition.

NSArray<NSNumber *> *filteredArray = [@[ @13, @42, @0 ] fbl_filter:^BOOL(NSNumber *value) {
  return value.integerValue > 0;
}];
XCTAssertEqualObjects(filteredArray, @[ @13, @42 ]);

First

Returns the first element in the collection that satisfies a condition.

NSNumber *firstValue = [@[ @13, @42, @100 ] fbl_first:^BOOL(NSNumber *value) {
  return value.integerValue > 20;
}];
XCTAssertEqualObjects(firstValue, @42);

FlatMap

Similar to map, but can also flatten a collection of collections.

NSArray<NSArray<NSNumber *> *> *originalArray = @[ @[ @13, @42 ], @[ @14, @43 ], @[] ];
NSArray<NSNumber *> *flatMappedArray = [originalArray fbl_flatMap:^id(NSArray<NSNumber *> *value) {
  return value.count > 0 ? value : nil;
}];
XCTAssertEqualObjects(flatMappedArray, @[ @13, @42, @14, @43 ]);

ForEach

Invokes a block on each element of the collection in the same order as a for-in loop.

[@[ @13, @42, @100 ] fbl_forEach:^(NSNumber *value) {
  // Invokes this block for values @13, @42, @100
}];

Map

Loops over a collection and applies the same operation to each element in the collection.

NSArray<NSString *> *mappedArray = [@[ @13, @42, @0 ] fbl_map:^id(NSNumber *value) {
  if (value.integerValue == 0) {
    return nil;
  }
  return value.stringValue;
}];
XCTAssertEqualObjects(mappedArray, @[ @"13", @"42", [NSNull null] ]);

Reduce

Combines all items in a collection to create a single value.

NSNumber *reducedValue =
    [@[ @13, @42, @100 ] fbl_reduce:@0
                            combine:^NSNumber *(NSNumber *accumulator, NSNumber *value) {
                              return @(accumulator.integerValue + value.integerValue);
                            }];
XCTAssertEqualObjects(reducedValue, @(13 + 42 + 100));

Zip

Creates an array of pairs built from the two input collections.

NSArray<NSArray *> *zippedArray = [@[ @13, @42, @101 ] fbl_zip:@[ @"100", @"14" ]];
XCTAssertEqualObjects(zippedArray, @[ @[ @13, @"100" ], @[ @42, @"14" ] ]);

Setup

CocoaPods

Add the following to your Podfile:

pod 'FunctionalObjC', '~> 1.0'

Or, if you would also like to include the tests:

pod 'FunctionalObjC', '~> 1.0', :testspecs => ['Tests']

Then, run pod install.

Import

Import the umbrella header as:

#import <FBLFunctional/FBLFunctional.h>

Or:

#import "FBLFunctional.h"

Or, the module:

@import FBLFunctional;