diff --git a/NSArray+LinqExtensions.h b/NSArray+LinqExtensions.h index 81361cf..cb7c774 100644 --- a/NSArray+LinqExtensions.h +++ b/NSArray+LinqExtensions.h @@ -29,21 +29,21 @@ typedef id (^LINQAccumulator)(id item, id aggregate); /** Various NSArray extensions that provide a Linq-style query API */ -@interface NSArray (QueryExtension) +@interface NSArray (QueryExtension) /** Filters a sequence of values based on a predicate. @param predicate The function to test each source element for a condition. @return An array that contains elements from the input sequence that satisfy the condition. */ -- (NSArray*) linq_where:(LINQCondition)predicate; +- (NSArray*) linq_where:(BOOL (NS_NOESCAPE ^)(ObjectType item))predicate; /** Projects each element of a sequence into a new form. @param selector A transform function to apply to each element. @return An array whose elements are the result of invoking the transform function on each element of source. */ -- (NSArray*) linq_select:(LINQSelector)transform; +- (NSArray*) linq_select:(id (NS_NOESCAPE ^)(ObjectType item))transform; /** Projects each element of a sequence into a new form. If the transform returns nil for any of the elements, the projection fails and returns nil. @@ -51,122 +51,122 @@ the projection fails and returns nil. @param selector A transform function to apply to each element. @return An array whose elements are the result of invoking the transform function on each element of source. */ -- (NSArray*)linq_selectAndStopOnNil:(LINQSelector)transform; +- (NSArray*)linq_selectAndStopOnNil:(id (NS_NOESCAPE ^)(ObjectType item))transform; /** Sorts the elements of a sequence in ascending order. @return An array whose elements are sorted in ascending order. */ -- (NSArray*) linq_sort; +- (NSArray *) linq_sort; /** Sorts the elements of a sequence in ascending order by using a specified keySelector. @param keySelector A selector that provides the 'key' which the array should by sorted by. @return An array whose elements are sorted in ascending order. */ -- (NSArray*) linq_sort:(LINQSelector)keySelector; +- (NSArray *) linq_sort:(id (NS_NOESCAPE ^)(ObjectType item))keySelector; /** Sorts the elements of a sequence in descending order. @return An array whose elements are sorted in descending order. */ -- (NSArray *)linq_sortDescending; +- (NSArray *)linq_sortDescending; /** Sorts the elements of a sequence in descending order by using a specified keySelector. @param keySelector A selector that provides the 'key' which the array should by sorted by. @return An array whose elements are sorted in descending order. */ -- (NSArray *)linq_sortDescending:(LINQSelector)keySelector; +- (NSArray *)linq_sortDescending:(id (NS_NOESCAPE ^)(ObjectType item))keySelector; /** Filters the elements of an an array based on a specified type. @param type The type to filter the elements of the sequence on. @return An array whose elements are all of the given type. */ -- (NSArray*) linq_ofType:(Class)type; +- (NSArray *) linq_ofType:(Class)type; /** Projects each element of a sequence to an NSArray and flattens the resulting sequences into one sequence. @param transform A transform function to apply to each element, this should return an NSArray. @return An array whose elements are the result of invoking the one-to-many transform function on each element of the input sequence. */ -- (NSArray*) linq_selectMany:(LINQSelector)transform; +- (NSArray*) linq_selectMany:(id (NS_NOESCAPE ^)(ObjectType item))transform; /** Returns distinct elements from a sequence. @return An array of distinct elements. */ -- (NSArray*) linq_distinct; +- (NSArray *) linq_distinct; /** Returns distinct elements from a sequence, where the given selector is used to specify the value to use for equality for each item. @param keySelector Specifies the value to use for equality for each item. @return An array of distinct elements. */ -- (NSArray*) linq_distinct:(LINQSelector)keySelector; +- (NSArray *) linq_distinct:(id (NS_NOESCAPE ^)(ObjectType item))keySelector; /** Applies an accumulator function over a sequence. The item in the array is used as the initial aggregate value. @param accumulator An accumulator function to be invoked on each element. @return The final accumulator value. */ -- (id) linq_aggregate:(LINQAccumulator)accumulator; +- (id) linq_aggregate:(id (NS_NOESCAPE ^)(ObjectType item, id aggregate))accumulator; /** Returns the first item from the source array, or nil if the array is empty. @return The first item from the source array, or nil if the array is empty. */ -- (id) linq_firstOrNil; +- (ObjectType) linq_firstOrNil; /** Returns the first item from the source array matching a predicate, or nil if there are no objects passing the test. @param predicate The function to test each source element for a condition. @return An item from the input sequence that satisfy the condition. */ -- (id)linq_firstOrNil:(LINQCondition)predicate; +- (ObjectType)linq_firstOrNil:(BOOL (NS_NOESCAPE ^)(ObjectType item))predicate; /** Returns the last item from the source array, or nil if the array is empty. @return The last item from the source array, or nil if the array is empty. */ -- (id) linq_lastOrNil; +- (ObjectType) linq_lastOrNil; /** Bypasses a specified number of elements in an array and then returns the remaining elements. @param count The number of elements to bypass. @return An array that contains the elements that occur after the specified index in the input array. */ -- (NSArray*) linq_skip:(NSUInteger)count; +- (NSArray *) linq_skip:(NSUInteger)count; /** Returns a specified number of contiguous elements from the start of an array. @param count The number of elements to take. @return An array that contains the specified number of elements from the start of the input array. */ -- (NSArray*) linq_take:(NSUInteger)count; +- (NSArray *) linq_take:(NSUInteger)count; /** Determines whether all the elements of the array satisfies a condition. @param condition The condition to test elements against. @return Whether all the elements of the array satisfies a condition. */ -- (BOOL) linq_all:(LINQCondition)condition; +- (BOOL) linq_all:(BOOL (NS_NOESCAPE ^)(ObjectType item))condition; /** Determines whether any of the elements of the array satisfies a condition. @param condition The condition to test elements against. @return Whether any of the elements of the array satisfies a condition. */ -- (BOOL) linq_any:(LINQCondition)condition; +- (BOOL) linq_any:(BOOL (NS_NOESCAPE ^)(ObjectType item))condition; /** Groups the elements of the array by keys provided by the given key selector. The returned dictionary will contain the keys that are the result of applying the key selector function to each item of the array, and the value for each key is an array of all the items that return the same key value. @param groupKeySelector Determines the group key for each item in the array @return A dictionary that groups the items via the given key selector. */ -- (NSDictionary*) linq_groupBy:(LINQSelector)groupKeySelector; +- (NSDictionary *> *) linq_groupBy:(id (NS_NOESCAPE ^)(ObjectType item))groupKeySelector; /** Transforms the source array into a dictionary by applying the given keySelector and valueSelector to each item in the array. @@ -174,21 +174,21 @@ the projection fails and returns nil. @param valueSelector A selector function that is applied to each item to determine the value it will have within the returned dictionary. @return A dictionary that is the result of applying the supplied selector functions to each item of the array. */ -- (NSDictionary*) linq_toDictionaryWithKeySelector:(LINQSelector)keySelector valueSelector:(LINQSelector)valueSelector; +- (NSDictionary*) linq_toDictionaryWithKeySelector:(id (NS_NOESCAPE ^)(ObjectType item))keySelector valueSelector:(id (NS_NOESCAPE ^)(ObjectType item))valueSelector; /** Transforms the source array into a dictionary by applying the given keySelectorto each item in the array. @param keySelector A selector function that is applied to each item to determine the key it will have within the returned dictionary. @return A dictionary that is the result of applying the supplied selector functions to each item of the array. */ -- (NSDictionary*) linq_toDictionaryWithKeySelector:(LINQSelector)keySelector; +- (NSDictionary *) linq_toDictionaryWithKeySelector:(id (NS_NOESCAPE ^)(ObjectType item))keySelector; /** Counts the number of elements in the array that satisfy the given condition. @param condition The condition to test elements against. @return The number of elements that satisfy the condition. */ -- (NSUInteger) linq_count:(LINQCondition)condition; +- (NSUInteger) linq_count:(BOOL (NS_NOESCAPE ^)(ObjectType item))condition; /** Concatonates the given array to the end of this array. @@ -201,7 +201,7 @@ the projection fails and returns nil. @return The reversed array. */ -- (NSArray*) linq_reverse; +- (NSArray *) linq_reverse; /** Sums the elements in the array. diff --git a/NSArray+LinqExtensions.m b/NSArray+LinqExtensions.m index ef32806..adbe37f 100644 --- a/NSArray+LinqExtensions.m +++ b/NSArray+LinqExtensions.m @@ -10,7 +10,7 @@ @implementation NSArray (QueryExtension) -- (NSArray *)linq_where:(LINQCondition)predicate +- (NSArray *)linq_where:(NS_NOESCAPE LINQCondition)predicate { NSMutableArray* result = [[NSMutableArray alloc] init]; for(id item in self) { @@ -21,7 +21,7 @@ - (NSArray *)linq_where:(LINQCondition)predicate return result; } -- (NSArray *)linq_select:(LINQSelector)transform +- (NSArray *)linq_select:(NS_NOESCAPE LINQSelector)transform andStopOnError:(BOOL)shouldStopOnError { NSMutableArray* result = [[NSMutableArray alloc] initWithCapacity:self.count]; @@ -47,20 +47,20 @@ - (NSArray *)linq_select:(LINQSelector)transform return result; } -- (NSArray *)linq_select:(LINQSelector)transform +- (NSArray *)linq_select:(NS_NOESCAPE LINQSelector)transform { return [self linq_select: transform andStopOnError: NO]; } -- (NSArray*)linq_selectAndStopOnNil:(LINQSelector)transform +- (NSArray*)linq_selectAndStopOnNil:(NS_NOESCAPE LINQSelector)transform { return [self linq_select: transform andStopOnError: YES]; } -- (NSArray *)linq_sort:(LINQSelector)keySelector +- (NSArray *)linq_sort:(NS_NOESCAPE LINQSelector)keySelector { return [self sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { id valueOne = keySelector(obj1); @@ -75,7 +75,7 @@ - (NSArray *)linq_sort return [self linq_sort:^id(id item) { return item;} ]; } -- (NSArray *)linq_sortDescending:(LINQSelector)keySelector +- (NSArray *)linq_sortDescending:(NS_NOESCAPE LINQSelector)keySelector { return [self sortedArrayUsingComparator:^NSComparisonResult(id obj2, id obj1) { id valueOne = keySelector(obj1); @@ -97,7 +97,7 @@ - (NSArray *)linq_ofType:(Class)type }]; } -- (NSArray *)linq_selectMany:(LINQSelector)transform +- (NSArray *)linq_selectMany:(NS_NOESCAPE LINQSelector)transform { NSMutableArray* result = [[NSMutableArray alloc] init]; for(id item in self) { @@ -119,7 +119,7 @@ - (NSArray *)linq_distinct return distinctSet; } -- (NSArray *)linq_distinct:(LINQSelector)keySelector +- (NSArray *)linq_distinct:(NS_NOESCAPE LINQSelector)keySelector { NSMutableSet* keyValues = [[NSMutableSet alloc] init]; NSMutableArray* distinctSet = [[NSMutableArray alloc] init]; @@ -135,7 +135,7 @@ - (NSArray *)linq_distinct:(LINQSelector)keySelector return distinctSet; } -- (id)linq_aggregate:(LINQAccumulator)accumulator +- (id)linq_aggregate:(NS_NOESCAPE LINQAccumulator)accumulator { id aggregate = nil; for (id item in self) { @@ -153,7 +153,7 @@ - (id)linq_firstOrNil return self.count == 0 ? nil : [self objectAtIndex:0]; } -- (id)linq_firstOrNil:(LINQCondition)predicate +- (id)linq_firstOrNil:(NS_NOESCAPE LINQCondition)predicate { for(id item in self) { if (predicate(item)) { @@ -185,7 +185,7 @@ - (NSArray*)linq_take:(NSUInteger)count return [self subarrayWithRange:range]; } -- (BOOL)linq_any:(LINQCondition)condition +- (BOOL)linq_any:(NS_NOESCAPE LINQCondition)condition { for (id item in self) { if (condition(item)) { @@ -195,7 +195,7 @@ - (BOOL)linq_any:(LINQCondition)condition return NO; } -- (BOOL)linq_all:(LINQCondition)condition +- (BOOL)linq_all:(NS_NOESCAPE LINQCondition)condition { for (id item in self) { if (!condition(item)) { @@ -205,7 +205,7 @@ - (BOOL)linq_all:(LINQCondition)condition return YES; } -- (NSDictionary*)linq_groupBy:(LINQSelector)groupKeySelector +- (NSDictionary*)linq_groupBy:(NS_NOESCAPE LINQSelector)groupKeySelector { NSMutableDictionary* groupedItems = [[NSMutableDictionary alloc] init]; for (id item in self) { @@ -222,7 +222,7 @@ - (NSDictionary*)linq_groupBy:(LINQSelector)groupKeySelector return groupedItems; } -- (NSDictionary *)linq_toDictionaryWithKeySelector:(LINQSelector)keySelector valueSelector:(LINQSelector)valueSelector +- (NSDictionary *)linq_toDictionaryWithKeySelector:(NS_NOESCAPE LINQSelector)keySelector valueSelector:(NS_NOESCAPE LINQSelector)valueSelector { NSMutableDictionary* result = [[NSMutableDictionary alloc] init]; for (id item in self) { @@ -239,12 +239,12 @@ - (NSDictionary *)linq_toDictionaryWithKeySelector:(LINQSelector)keySelector val return result; } -- (NSDictionary *)linq_toDictionaryWithKeySelector:(LINQSelector)keySelector +- (NSDictionary *)linq_toDictionaryWithKeySelector:(NS_NOESCAPE LINQSelector)keySelector { return [self linq_toDictionaryWithKeySelector:keySelector valueSelector:nil]; } -- (NSUInteger)linq_count:(LINQCondition)condition +- (NSUInteger)linq_count:(NS_NOESCAPE LINQCondition)condition { return [self linq_where:condition].count; } diff --git a/NSDictionary+LinqExtensions.h b/NSDictionary+LinqExtensions.h index 4e33ef2..53e2182 100644 --- a/NSDictionary+LinqExtensions.h +++ b/NSDictionary+LinqExtensions.h @@ -15,49 +15,49 @@ typedef BOOL (^LINQKeyValueCondition)(id key, id value); /** Various NSDictionary extensions that provide a Linq-style query API */ -@interface NSDictionary (QueryExtension) +@interface NSDictionary (QueryExtension) /** Filters a dictionary based on a predicate. @param predicate The function to test each source key-value pair for a condition. @return A dictionary that contains key-value pairs from the input dictionary that satisfy the condition. */ -- (NSDictionary*) linq_where:(LINQKeyValueCondition)predicate; +- (NSDictionary *) linq_where:(BOOL (NS_NOESCAPE ^)(KeyType key, ObjectType value))predicate; /** Projects each element of the dictionary into a new form. @param selector A transform function to apply to each element. @return A dicionary whose elements are the result of invoking the transform function on each key-value pair of source. */ -- (NSDictionary*) linq_select:(LINQKeyValueSelector)selector; +- (NSDictionary*) linq_select:(id (NS_NOESCAPE ^)(KeyType key, ObjectType value))selector; /** Projects each element of the dictionary to a new form, which is used to populate the returned array. @param selector A transform function to apply to each element. @return An array whose elements are the result of invoking the transform function on each key-value pair of source. */ -- (NSArray*) linq_toArray:(LINQKeyValueSelector)selector; +- (NSArray*) linq_toArray:(id (NS_NOESCAPE ^)(KeyType key, ObjectType value))selector; /** Determines whether all of the key-value pairs of the dictionary satisfies a condition. @param condition The condition to test key-value pairs against. @return Whether any of the element of the dictionary satisfies a condition. */ -- (BOOL) linq_all:(LINQKeyValueCondition)condition; +- (BOOL) linq_all:(BOOL (NS_NOESCAPE ^)(KeyType key, ObjectType value))condition; /** Determines whether any of the key-value pairs of the dictionary satisfies a condition. @param condition The condition to test key-value pairs against. @return Whether any of the element of the dictionary satisfies a condition. */ -- (BOOL) linq_any:(LINQKeyValueCondition)condition; +- (BOOL) linq_any:(BOOL (NS_NOESCAPE ^)(KeyType key, ObjectType value))condition; /** Counts the number of key-value pairs that satisfy the given condition. @param condition The condition to test key-value pairs against. @return The number of elements that satisfy the condition. */ -- (NSUInteger) linq_count:(LINQKeyValueCondition)condition; +- (NSUInteger) linq_count:(BOOL (NS_NOESCAPE ^)(KeyType key, ObjectType value))condition; /** Merges the contents of this dictionary with the given dictionary. For any duplicates, the value from the source dictionary will be used. diff --git a/NSDictionary+LinqExtensions.m b/NSDictionary+LinqExtensions.m index ce00b06..8d84194 100644 --- a/NSDictionary+LinqExtensions.m +++ b/NSDictionary+LinqExtensions.m @@ -10,7 +10,7 @@ @implementation NSDictionary (QueryExtension) -- (NSDictionary *)linq_where:(LINQKeyValueCondition)predicate +- (NSDictionary *)linq_where:(NS_NOESCAPE LINQKeyValueCondition)predicate { NSMutableDictionary* result = [[NSMutableDictionary alloc] init]; [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { @@ -21,7 +21,7 @@ - (NSDictionary *)linq_where:(LINQKeyValueCondition)predicate return result; } -- (NSDictionary *)linq_select:(LINQKeyValueSelector)selector +- (NSDictionary *)linq_select:(NS_NOESCAPE LINQKeyValueSelector)selector { NSMutableDictionary* result = [[NSMutableDictionary alloc] init]; [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { @@ -34,7 +34,7 @@ - (NSDictionary *)linq_select:(LINQKeyValueSelector)selector return result; } -- (NSArray *)linq_toArray:(LINQKeyValueSelector)selector +- (NSArray *)linq_toArray:(NS_NOESCAPE LINQKeyValueSelector)selector { NSMutableArray* result = [[NSMutableArray alloc] init]; [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { @@ -46,7 +46,7 @@ - (NSArray *)linq_toArray:(LINQKeyValueSelector)selector return result; } -- (BOOL)linq_all:(LINQKeyValueCondition)condition +- (BOOL)linq_all:(NS_NOESCAPE LINQKeyValueCondition)condition { __block BOOL all = TRUE; [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { @@ -58,7 +58,7 @@ - (BOOL)linq_all:(LINQKeyValueCondition)condition return all; } -- (BOOL)linq_any:(LINQKeyValueCondition)condition +- (BOOL)linq_any:(NS_NOESCAPE LINQKeyValueCondition)condition { __block BOOL any = FALSE; [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { @@ -70,7 +70,7 @@ - (BOOL)linq_any:(LINQKeyValueCondition)condition return any; } -- (NSUInteger)linq_count:(LINQKeyValueCondition)condition +- (NSUInteger)linq_count:(NS_NOESCAPE LINQKeyValueCondition)condition { return [self linq_where:condition].count; }