BLKFunctional 0.2.0

BLKFunctional 0.2.0

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Oliver Staats.



  • By
  • olistaats

About

BLKFunctional is a small library to enable a bit of functional style programming in objective-c.

Categories on NSArray and NSString uses the library prefix "blk_", to avoid namespace clashes. The categories add the minimum number of functions to avoid namespace pollution.

Example

// Assuming the following array
NSArray *numberArray = @[@2, @9, @66];

// 1. Conjoin, can join arrays, sets, dictionaries and objects
NSArray *arr1  = [numberArray blk_conjoin:@6 and:@7 ];    // arr1 = @[@2, @9, @66, @6, @7]
NSArray *arr2  = [numberArray blk_conjoin:@[@6, @7] ];    // arr2 = @[@2, @9, @66, @6, @7]

NSString *str1 = [@"[2" blk_conjoin:@", 9" and:@", 66]"]; // str1 = @"[2, 9, 66]"
NSString *str2 = [@"["  blk_conjoin:numberArray interpose:@", " end:@"]" ];
                                                          // str2 = @"[2, 9, 66]"

// 2. Convenience expression macros
// Below, the type of argument "p1" is NSNumber, so ide code completion is available
NSArray *arr3 = [numberArray blk_map:BLKFnN(@([p1 doubleValue] + 2)) ];

// 3. Functions on NSArray
NSArray *arr4 = [numberArray blk_map:^(NSNumber *p1) {
    return @([p1 doubleValue] + 2);
}];

// 4. (OR) If you absolutely can't have a category
NSArray *arr5 = [BLKFunction mapArray:numberArray withBlock:^(NSNumber *p1) {
    return @([p1 doubleValue] + 2);
}];

// For category names and expression macros, #import BLKCatFunction.h
// (OR) To forgo categories, #import BLKFunction.h (and optionally, #import BLKMacro.h)

Available

Available on NSArray as Category methods (with prefix "blk_")

  • take, drop, filter, map, reduce, fold, conjoin

Available on NSString as Category method (with prefix "blk_")

  • conjoin

Additionally, available via BLKFunction.h (already imported if you are using categories)

  • array: indexed versions of map, fold
  • dictionary: fold, indexed fold
  • shuffle, reverse
  • fold with options to enumerate in reverse and/or parallelize

Enumeration "Stop" short circuit (like on enumerateObjectsUsingBlock:) Via returning constant BLKStopProcessing, available to

  • map, reduce, fold

pod 'BLKFunctional'

Example

  • There's an included example project ExampleBLKFunctional.

Status

  • conjoin is still experimental