https://github.com/google/functional-objc
Functional operators for Objective-C
https://github.com/google/functional-objc
functional objective-c operators
Last synced: 2 months ago
JSON representation
Functional operators for Objective-C
- Host: GitHub
- URL: https://github.com/google/functional-objc
- Owner: google
- License: apache-2.0
- Created: 2018-01-19T02:49:58.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T14:23:21.000Z (over 2 years ago)
- Last Synced: 2025-03-22T00:41:42.842Z (3 months ago)
- Topics: functional, objective-c, operators
- Language: Objective-C
- Homepage:
- Size: 39.1 KB
- Stars: 348
- Watchers: 12
- Forks: 20
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](LICENSE)
[](https://travis-ci.org/google/functional-objc)# 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.```objectivec
NSArray *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.
```objectivec
NSNumber *firstValue = [@[ @13, @42, @100 ] fbl_first:^BOOL(NSNumber *value) {
return value.integerValue > 20;
}];
XCTAssertEqualObjects(firstValue, @42);
```### FlatMap
Similar to [`map`](#map), but can also flatten a collection of collections.
```objectivec
NSArray *> *originalArray = @[ @[ @13, @42 ], @[ @14, @43 ], @[] ];
NSArray *flatMappedArray = [originalArray fbl_flatMap:^id(NSArray *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.```objectivec
[@[ @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.```objectivec
NSArray *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.
```objectivec
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.
```objectivec
NSArray *zippedArray = [@[ @13, @42, @101 ] fbl_zip:@[ @"100", @"14" ]];
XCTAssertEqualObjects(zippedArray, @[ @[ @13, @"100" ], @[ @42, @"14" ] ]);
```## Setup
### CocoaPods
Add the following to your `Podfile`:
```ruby
pod 'FunctionalObjC', '~> 1.0'
```Or, if you would also like to include the tests:
```ruby
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](https://github.com/Carthage/Carthage#getting-started).
### Import
Import the umbrella header as:
```objectivec
#import
```Or:
```objectivec
#import "FBLFunctional.h"
```Or, the module:
```objectivec
@import FBLFunctional;
```