Skip to content

fancybits/functional-objc

 
 

Repository files navigation

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;

About

Functional operators for Objective-C

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Objective-C 92.1%
  • Starlark 3.1%
  • Swift 2.5%
  • Ruby 2.3%