Skip to content
Functional operators for Objective-C
Objective-C Python Ruby Swift
Branch: master
Clone or download
Latest commit c5755c1 Jul 4, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
FBLFunctional.xcodeproj Update deployment targets Feb 28, 2018
Sources/FBLFunctional Fix Xcode 10 warnings related to NS_NOESCAPE Jul 9, 2018
Tests/FBLFunctionalTests
.clang-format Initial Jan 19, 2018
.gitignore Carthage support Feb 28, 2018
.hgignore Carthage support Feb 28, 2018
.travis.yml Support Travis CI Feb 4, 2018
BUILD Update deployment targets Feb 28, 2018
CONTRIBUTING.md Adds tests to the podspec. Updates readme. Jan 20, 2018
FBLFunctional.h Initial Jan 19, 2018
FunctionalObjC.podspec Update deployment targets Feb 28, 2018
LICENSE Initial Jan 19, 2018
Package.swift Add FBL prefix in Package.Swift Jan 20, 2018
README.md Carthage support Feb 28, 2018
WORKSPACE Update dependencies to work with bazel 15.0. Jul 3, 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.

Carthage

Add the following to your Cartfile:

github "google/functional-objc"

Then, run carthage update and follow the rest of instructions.

Import

Import the umbrella header as:

#import <FBLFunctional/FBLFunctional.h>

Or:

#import "FBLFunctional.h"

Or, the module:

@import FBLFunctional;
You can’t perform that action at this time.