Asterism
A simple functional toolbelt for Objective-C
Asterism is a yet another functional toolbelt for
Objective-C. It tries to be typesafe and elegant.
Using common higher-order functions such as
map
, reduce
and
filter
, Asterism allows you to manipulate
Foundation's data structures with ease.
It makes use of overloaded C-Functions to keep its interface
simple while maintaining compile-time safety.
For instance, ASTEach
takes different blocks
depending on the data structure it operates on:
ASTEach(@[ @"a", @"b", @"c" ], ^(NSString *letter) {
NSLog(@"%@", letter);
});
ASTEach(@[ @"a", @"b", @"c" ], ^(NSString *letter, NSUInteger index) {
NSLog(@"%u: %@", index, letter);
});
ASTEach(@{ @"foo": @"bar" }, ^(NSString *key, NSString *value) {
NSLog(@"%@: %@", key, value);
});
Both BlocksKit and Objective-Sugar rely on categories, this
means they can't create methods that operate on protocols,
such as NSFastEnumeration
.
Asterism is currently exclusively using overloaded
C-Functions.
BlocksKit also adds optional additional block-support to a
wide range of UIKit classes, whereas Asterism's focus is
data structures.
That being said, both are well tested and written by smart
people. If Asterism should not be your thing, do check them
out.
How to install Asterism
The recommended way to install Asterism is using the
excellent CocoaPods:
pod 'Asterism', '1.0.0-RC3'
Alternatively, you can
find the code on GitHub.
Asterism was written by Robert
Böhnke and is MIT licensed.
ASTAll
BOOL ASTAll(NSDictionary *dict, BOOL(^block)(id obj))
Tests if all values in a dictionary pass a test.
Returns YES
if all values in dict
pass the test block
.
BOOL ASTAll(id<NSFastEnumeration> collection, BOOL(^block)(id obj))
Tests if all elements in a collection pass a test.
Returns YES
if all elements in collection
pass the test block
.
ASTAny
BOOL ASTAny(NSDictionary *dict, BOOL(^block)(id obj))
Tests if any value in a dictionary passes a test.
Returns YES
if any of the values in dict
passes the test block
.
BOOL ASTAny(id<NSFastEnumeration> collection, BOOL(^block)(id obj))
Tests if any element in a collection passes a test.
Returns YES
if any of the elements in collection
passes the test block
.
ASTDifference
NSArray *ASTDifference(NSArray *array, NSArray *other)
Returns the difference between two arrays.
-
array
An array of elements.
-
other
An array of elements.
Returns an array containing the elements of array
that are not present in other
. The order is being maintained.
NSSet *ASTDifference(NSSet *set, NSSet *other)
Returns the difference between two sets.
-
set
A set of elements.
-
other
A set of elements.
Returns a set containing the elements of set
that are not present in other
.
NSOrderedSet *ASTDifference(NSOrderedSet *set, NSOrderedSet *other)
Returns the difference between two ordered sets.
Returns an ordered set containing the elements of set
that are not present in other
.
ASTEach
void ASTEach(NSArray *array, void(^iterator)(id obj))
Iterates over all elements of an array.
void ASTEach(NSArray *array, void(^iterator)(id obj, NSUInteger idx))
Iterates over all elements of an array, as well as their indexes.
void ASTEach(NSDictionary *dict, void(^iterator)(id obj))
Iterates over all values of a dictionary.
void ASTEach(NSDictionary *dict, void(^iterator)(id key, id obj))
Iterates over all keys and values of a dictionary.
void ASTEach(NSOrderedSet *set, void(^iterator)(id obj, NSUInteger idx))
Iterates over all elements of an ordered set, as well as their indexes.
void ASTEach(id<NSFastEnumeration> enumerable, void(^iterator)(id obj))
Iterates over elements in a collection.
ASTEmpty
BOOL ASTEmpty(NSArray *array)
Returns YES if array
is empty.
BOOL ASTEmpty(NSDictionary *dictionary)
Returns YES if dictionary
is empty.
BOOL ASTEmpty(NSSet *set)
Returns YES if set
is empty.
BOOL ASTEmpty(NSOrderedSet *set)
Returns YES if set
is empty.
BOOL ASTEmpty(id<NSFastEnumeration> collection)
Returns YES if collection
is empty.
ASTExtend
NSDictionary *ASTExtend(NSDictionary *dict, NSDictionary *source)
Extends a dictionary with values from another dictionary.
Returns a new dictionary that contains a union of key-value-pairs of dict
and source
. Key-value-pairs of source
will have precedence over those taken from dict
.
ASTFilter
NSArray *ASTFilter(NSArray *array, BOOL(^block)(id obj))
Filters out the elements of an array that fail a test.
Returns an array of all values in array
that pass the test. The order is being maintained.
NSArray *ASTFilter(NSArray *array, BOOL(^block)(id obj, NSUInteger idx))
Filters out the elements of an array that fail a test.
Returns an array of all values in array
that pass the test. The order is being maintained.
NSDictionary *ASTFilter(NSDictionary *dict, BOOL(^block)(id obj))
Filters out the values of a dictionary that fail a test.
Returns a dictionary of the keys and values in dict
for which the values passed the test.
NSDictionary *ASTFilter(NSDictionary *dict, BOOL(^block)(id key, id obj))
Filters out the keys and values of a dictionary that fail a test.
Returns a dictionary of the keys and values in dict
that passed the test.
NSSet *ASTFilter(NSSet *set, BOOL(^block)(id obj))
Filters out the elements of a set that fail a test.
Returns a set of all values in set
that pass the test.
NSOrderedSet *ASTFilter(NSOrderedSet *set, BOOL(^block)(id obj))
Filters out the elements of an ordered set that fail a test.
Returns an ordered set of all values in set
that pass the test.
NSOrderedSet *ASTFilter(NSOrderedSet *array, BOOL(^block)(id obj, NSUInteger idx))
Filters out the elements of an ordered set that fail a test.
Returns an ordered set of all values in set
that pass the test. The order is being maintained.
ASTFind
id ASTFind(NSArray *array, BOOL(^block)(id obj))
Finds an element in an array.
Returns the first item in array
for which block
returns YES
or nil
if no such value was found.
id ASTFind(NSArray *array, BOOL(^block)(id obj, NSUInteger idx))
Finds an element in an array.
Returns the first item in array
for which block
returns YES
or nil
if no such value was found.
id ASTFind(NSDictionary *dict, BOOL(^block)(id obj))
Finds a value in a dictionary.
Returns any value in dict
for which block
returns YES
or nil
if no such value was found.
id ASTFind(NSDictionary *dict, BOOL(^block)(id key, id obj))
Finds a value in a dictionary.
Returns any value in dict
for which block
returns YES
or nil
if no such value was found.
id ASTFind(id<NSFastEnumeration> collection, BOOL(^block)(id obj))
Finds a value in a collection.
Returns a value in collection
for which block
returns YES
or nil
if no such value was found. If collection
makes an order guarantee, ASTFind
will return the first value matching the search criteria.
ASTGroupBy
NSDictionary *ASTGroupBy(NSDictionary *dict, id<NSCopying> (^block)(id obj))
Groups the values of a dictionary using a block.
-
dict
A dictionary of elements.
-
block
A block that takes a value of dict
as its only argument and returns a key by which to group the value. The return value is required to implement NSCopying. The block must not be nil.
Returns a dictionary that maps the keys returned by block
to a set of all values of dict
that share the same key.
NSDictionary *ASTGroupBy(NSDictionary *dict, NSString *keyPath)
Groups the values of a dictionary by their value for a given key path.
Returns a dictionary that maps the keys that the values in dict
return for keyPath
to a set of all values of dict
that share the same key.
NSDictionary *ASTGroupBy(id<NSFastEnumeration> collection, id<NSCopying> (^block)(id obj))
Groups the elements in a collection using a block.
-
collection
An object that implements NSFastEnumeration.
-
block
A block that takes an element in collection
as its only argument and returns a key by which to group the element. The return value is required to implement NSCopying. The block must not be nil.
Examples
NSArray *numbers = @[ @1, @2, @3, @4, @5 ];
NSDictionary *grouped = ASTGroupBy(numbers, ^(NSNumber *number){
return number.integerValue % 2 == 0 ? @"even" : @"odd";
});
grouped[@"even"]; // { @2, @4 }
grouped[@"odd"]; // { @1, @3, @5 }
Returns a dictionary that maps the keys returned by block
to a set of all values in collection
that share the same key.
NSDictionary *ASTGroupBy(id<NSFastEnumeration> collection, NSString *keyPath)
Groups the elements in a collection by their value for a given key path.
Examples
NSArray *numbers = @[ @"foo", @"bar", @"surprise" ];
NSDictionary *grouped = ASTGroupBy(numbers, @"length");
grouped[@3]; // { @"foo", @"bar" }
grouped[@8]; // { @"surprise" }
Returns a dictionary that maps the values the elements return for keyPath
to a set of all values in collection
that share the same key.
ASTHead
id ASTHead(NSArray *array)
Returns the first element of an array.
-
array
An array of elements.
Returns the first element or nil
if the array is empty.
id ASTHead(NSOrderedSet *set)
Returns the first element of an ordered set.
Returns the first element or nil
if the ordered set is empty.
ASTIndexBy
NSDictionary *ASTIndexBy(NSDictionary *dict, id<NSCopying> (^block)(id obj))
Indexes the values of a dictionary using a block.
-
dict
A dictionary of elements.
-
block
A block that takes a value of dict
as its only argument and returns a key by which to index the element. The return value is required to implement NSCopying. The block must not be nil.
Returns a dictionary that maps the keys returned by block
to the respective input value. If block
returns the same value for multiple values, an arbitrary value is chosen.
NSDictionary *ASTIndexBy(NSDictionary *dict, NSString *keyPath)
Indexes the values of a dictionary by their value for a given key path.
Returns a dictionary that maps the values the elements return for keyPath
to the respective input value. If multiple values return the same value for keyPath
, an arbitrary element is chosen.
NSDictionary *ASTIndexBy(id<NSFastEnumeration> collection, id<NSCopying> (^block)(id obj))
Indexes the elements of a collection using a block.
-
collection
An object that implements NSFastEnumeration.
-
block
A block that takes an element in collection
as its only argument and returns a key by which to index the element. The return value is required to implement NSCopying. The block must not be nil.
Examples
NSArray *strings = @[ @"foo", @"bar" ];
NSDictionary *indexed = ASTIndexBy(strings, ^(NSString *string){
return @([string characterAtIndex:0]);
});
indexed[@"f"]; // @"foo"
indexed[@"b"]; // @"bar"
Returns a dictionary that maps the keys returned by block
to the respective input value. If block
returns the same value for multiple values, an arbitrary value is chosen.
NSDictionary *ASTIndexBy(id<NSFastEnumeration> collection, NSString *keyPath)
Indexes the elements in a collection by their value for a given key path.
Examples
NSArray *strings = @[ @"a", @"ab", @"abc" ];
NSDictionary *indexed = ASTIndexBy(strings, @"length");
indexed[@1]; // @"a"
indexed[@2]; // @"ab"
indexed[@3]; // @"abc"
Returns a dictionary that maps the values the elements return for keyPath
to the respective input value. If multiple values return the same value for keyPath
, an arbitrary element is chosen.
ASTIndexOf
NSUInteger ASTIndexOf(NSArray *array, id obj)
Finds the index of an object in an array.
-
array
An array of elements.
-
obj
The object to find.
Returns the first index of obj in array or NSNotFound if the object could not be found.
NSUInteger ASTIndexOf(NSOrderedSet *set, id obj)
Finds the index of an object in an ordered set.
Returns the first index of obj in set or NSNotFound if the object could not be found.
NSUInteger ASTIndexOf(id<NSFastEnumeration> collection, id obj)
Finds the index of an object in a collection.
Returns the first index of obj in collection or NSNotFound if the object could not be found. If collection does not make a guarantee regarding its order, such as NSSet or NSDictionary, the meaning of the return value is undefined.
ASTIntersection
NSArray *ASTIntersection(NSArray *array, NSArray *other)
Returns the intersection of two arrays.
-
array
An array of elements.
-
other
An array of elements.
Returns an array containing the elements of array
that are also present in other
. The order is being maintained.
NSSet *ASTIntersection(NSSet *set, NSSet *other)
Returns the difference between two sets.
-
set
A set of elements.
-
other
A set of elements.
Returns a set containing the elements of set
that are also present in other
.
NSOrderedSet *ASTIntersection(NSOrderedSet *set, NSOrderedSet *other)
Returns the difference between two ordered sets.
Returns a set containing the elements of set
that are also present in other
. The order is being maintained.
ASTMap
NSArray *ASTMap(NSArray *array, id(^block)(id obj))
Maps a block across an array.
Returns an array that contains all values of array
after block
has been applied. If block
returns nil
, the element is not present in the returned array. The order is being maintained.
NSArray *ASTMap(NSArray *array, id(^block)(id obj, NSUInteger idx))
Maps a block across an array.
Returns an array that contains all values of array
after block
has been applied. If block
returns nil
, the element is not present in the returned array. The order is being maintained.
NSDictionary *ASTMap(NSDictionary *dict, id(^block)(id obj))
Maps a block across a dictionary.
Returns a dictionary that contains all keys and values of dict
after block
has been applied to the value. If block
returns nil
, the key and value are not present in the returned dictionary.
NSDictionary *ASTMap(NSDictionary *dict, id(^block)(id key, id obj))
Maps a block across a dictionary.
Returns a dictionary that contains all keys and values of dict
after block
has been applied to them. If block
returns nil
, the key and value are not present in the returned dictionary.
NSSet *ASTMap(NSSet *set, id(^block)(id obj))
Maps a block across a set.
Returns a set that contains all values of set
after block
has been applied. If block
returns nil
, the element is not present in the returned set.
NSOrderedSet *ASTMap(NSOrderedSet *set, id(^block)(id obj))
Maps a block across an ordered set.
Returns an ordered set that contains all values of set
after block
has been applied. If block
returns nil
, the element is not present in the returned set. The order is being maintained.
NSOrderedSet *ASTMap(NSOrderedSet *array, id(^block)(id obj, NSUInteger idx))
Maps a block across an ordered set.
Returns an ordered set that contains all values of set
after block
has been applied. If block
returns nil
, the element is not present in the returned set. The order is being maintained.
ASTMin
id ASTMin(NSDictionary *dict)
Returns the minimum of the values of a dictionary by invoking -compare:.
Returns the minimum of the values of dict
comparing all values by invoking -compare:.
id ASTMin(NSDictionary *dict, NSComparator comparator)
Returns the minimum of the values of a dictionary by using an NSComparator.
Returns the minimum of the values of dict
comparing all values using comparator
.
id ASTMin(id<NSFastEnumeration> collection)
Returns the minimum of a collection by invoking -compare:.
Returns the minimum of the collection by comparing all values by invoking -compare:.
id ASTMin(id<NSFastEnumeration> collection, NSComparator comparator)
Returns the minimum of a collection by using an NSComparator.
Returns the minimum of the collection by comparing all values using comparator
.
ASTMax
id ASTMax(NSDictionary *dict)
Returns the maximum of the values of a dictionary by invoking -compare:.
Returns the maximum of the values of dict
comparing all values by invoking -compare:.
id ASTMax(NSDictionary *dict, NSComparator comparator)
Returns the maximum of the values of a dictionary by using an NSComparator.
Returns the maximum of the values of dict
comparing all values using comparator
.
id ASTMax(id<NSFastEnumeration> collection)
Returns the maximum of a collection by invoking -compare:.
Returns the maximum of the collection by comparing all values by invoking -compare:.
id ASTMax(id<NSFastEnumeration> collection, NSComparator comparator)
Returns the maximum of a collection by using an NSComparator.
Returns the maximum of the collection by comparing all values using comparator
.
ASTNegate
BOOL (^ASTNegate(BOOL(^block)(id)))(id)
Negates a block.
Returns a new block of the same type that returns the opposite of what block
returns.
BOOL (^ASTNegate(BOOL(^block)(id, id)))(id, id)
Negates a block.
Returns a new block of the same type that returns the opposite of what block
returns.
BOOL (^ASTNegate(BOOL(^block)(id, NSUInteger)))(id, NSUInteger)
Negates a block.
Returns a new block of the same type that returns the opposite of what block
returns.
ASTPluck
NSDictionary *ASTPluck(NSDictionary *dictionary, NSString *keyPath)
Extracts a value for a given key path from all values of a dictionary.
Returns a dictionary mapping the original keys to the values that the values in dict
return for keyPath
. If a value returns nil when invoked with -valueForKeyPath:, it is not present in the returned dictionary.
NSArray *ASTPluck(id<NSFastEnumeration> collection, NSString *keyPath)
Extracts a value for a given key path from all elements in a collection.
Returns an array of the values that the elements in collection
return for keyPath
. If an element returns nil when invoked with -valueForKeyPath:, it is not present in the returned array. If possible, the order is being maintained.
ASTReduce
id ASTReduce(NSDictionary *dict, id(^block)(id memo, id obj))
Reduces a dictionary to a single value.
-
dict
An object that implements NSFastEnumeration.
-
block
A block that takes two arguments and returns an object. The first argument is its last return value or a value of dict
when it is called for the first time. The second argument is the next value of dict
. The block must not be nil.
Returns the last return value of block
once it reached the end of dict
. If dict
has only one value, block
is never invoked and that value. If dict
is empty, nil is returned.
id ASTReduce(NSDictionary *dict, id memo, id(^block)(id memo, id obj))
Reduces a dictionary to a single value.
-
dict
An object that implements NSFastEnumeration.
-
memo
The first argument to block
when it is invoked for the first time.
-
block
A block that takes two arguments and returns an object. The first argument is its last return value or memo
when it is called for the first time. The second argument is the next value of dict
. The block must not be nil.
Returns the last return value of block
once it reached the end of dict
. If dict
is empty, memo
is returned.
id ASTReduce(id<NSFastEnumeration> collection, id(^block)(id memo, id obj))
Reduces a collection to a single value.
-
collection
An object that implements NSFastEnumeration.
-
block
A block that takes two arguments and returns an object. The first argument is its last return value or the first element in the collection
when it is called for the first time. The second argument is the next value in the collection, starting with the second one. The block must not be nil.
Examples
NSString *(^concat)(NSString *, NSString *) = ^(NSString *a, NSString *b) {
return [a stringByAppendingString:b];
};
// Equivalent to [@"a" stringByAppendingString:@"b"];
ASTReduce(@[ @"a", @"b" ], concat);
// Equivalent to [[@"a" stringByAppendingString:@"b"] stringByAppendingString:@"c"];
ASTReduce(@[ @"a", @"b", @"c" ], concat);
Returns the last return value of block
once it reached the end of collection
. If collection
has only one element, block
is never invoked and the first element is returned. If collection
is empty, nil is returned.
id ASTReduce(id<NSFastEnumeration> collection, id memo, id(^block)(id memo, id obj))
Reduces a collection to a single value.
-
collection
An object that implements NSFastEnumeration.
-
memo
The first argument to block
when it is invoked for the first time.
-
block
A block that takes two arguments and returns an object. The first argument is its last return value or memo
when it is called for the first time. The second argument is the next value in the collection, starting with the first. The block must not be nil.
Returns the last return value of block
once it reached the end of collection
. If collection
is empty, memo
is returned.
ASTReject
NSArray *ASTReject(NSArray *array, BOOL(^block)(id obj))
Filters out the elements of an array that pass a test.
Returns an array of all values in array
that fail the test. The order is being maintained.
NSArray *ASTReject(NSArray *array, BOOL(^block)(id obj, NSUInteger idx))
Filters out the elements of an array that pass a test.
Returns an array of all values in array
that fail the test. The order is being maintained.
NSDictionary *ASTReject(NSDictionary *dict, BOOL(^block)(id obj))
Filters out the values of a dictionary that pass a test.
Returns a dictionary of the keys and values in dict
for which the values failed the test.
NSDictionary *ASTReject(NSDictionary *dict, BOOL(^block)(id key, id obj))
Filters out the keys and values of a dictionary that pass a test.
Returns a dictionary of the keys and values in dict
that fail the test.
NSSet *ASTReject(NSSet *set, BOOL(^block)(id obj))
Filters out the elements of a set that pass a test.
Returns a set of all values in set
that fail the test.
NSOrderedSet *ASTReject(NSOrderedSet *set, BOOL(^block)(id obj))
Filters out the elements of an ordered set that pass a test.
Returns an ordered set of all values in set
that fail the test.
NSOrderedSet *ASTReject(NSOrderedSet *set, BOOL(^block)(id obj, NSUInteger idx))
Filters out the elements of an ordered set that pass a test.
Returns an ordered set of all values in set
that fail the test. The order is being maintained.
ASTSize
NSUInteger ASTSize(NSArray *array)
The number of values in an array.
-
array
An array of elements.
Returns the size of array
.
NSUInteger ASTSize(NSDictionary *dictionary)
The number of values in a dictionary.
Returns the size of dictionary
.
NSUInteger ASTSize(NSSet *set)
The number of values in a set.
Returns the size of set
.
NSUInteger ASTSize(NSOrderedSet *set)
The number of values in an ordered set.
Returns the size of set
.
NSUInteger ASTSize(id<NSFastEnumeration> collection)
Counts the number of elements in a collection.
Returns the size of collection
in O(n).
ASTSort
NSArray *ASTSort(NSArray *array)
Sorts an array using -compare:
.
-
array
An array of elements.
Returns a copy of array
, sorted using -compare:
.
NSArray *ASTSort(NSArray *array, NSComparator comparator)
Sorts an array using a custom comparator.
Returns a copy of array
, sorted using comparator
.
NSOrderedSet *ASTSort(NSOrderedSet *set)
Sorts an ordered set using -compare:
.
-
set
An array of elements.
Returns a copy of set
, sorted using -compare:
.
NSOrderedSet *ASTSort(NSOrderedSet *set, NSComparator comparator)
Sorts an ordered set using a custom comparator.
Returns a copy of set
, sorted using comparator
.
ASTTail
NSArray *ASTTail(NSArray *array)
Returns all elements of an array after the first one.
-
array
An array of elements.
Returns all elements after the first one. If the array has less than one element, an empty array is returned.
NSOrderedSet *ASTTail(NSOrderedSet *set)
Returns all elements of an ordered set after the first one.
Returns all elements after the first one. If the set has less than one element, an empty ordered set is returned.
ASTUnion
NSArray *ASTUnion(NSArray *array, NSArray *other)
Returns the union of two arrays.
-
array
An array of elements.
-
other
An array of elements.
Returns an array containing all elements of array
, concatenated with all elements of other
not already present in array
. The order is being maintained.
NSSet *ASTUnion(NSSet *set, NSSet *other)
Returns the union two sets.
-
set
A set of elements.
-
other
A set of elements.
Returns a set containing the elements of set
and other
.
NSOrderedSet *ASTUnion(NSOrderedSet *set, NSOrderedSet *other)
Returns the union of two ordered sets.
Returns an orderd set containing all elements of set
, concatenated with all elements of other
not already present in set
. The order is being maintained.
ASTWithout
NSArray *ASTWithout(NSArray *array, id obj)
Filters out the elements of an array that are equal to a given value.
Returns an array of all values in array
that are not equal to obj
. The order is being maintained.
NSSet *ASTWithout(NSSet *set, id obj)
Filters out the elements of a set that are equal to a given value.
Returns a set of all values in set
that are not equal to obj
.
NSOrderedSet *ASTWithout(NSOrderedSet *set, id obj)
Filters out the elements of an ordered set that are equal to a given value.
Returns an ordered set of all values in set
that are not equal to obj
. The order is being maintained.