Coder Social home page Coder Social logo

jacobjennings / functional-objc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from google/functional-objc

0.0 1.0 0.0 34 KB

Functional operators for Objective-C

License: Apache License 2.0

Python 2.85% Ruby 2.35% Swift 2.29% Objective-C 92.52%

functional-objc's Introduction

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;

functional-objc's People

Contributors

shoumikhin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.