Permalink
Browse files

add reduce function

  • Loading branch information...
1 parent c0ce3d5 commit 2620c010df2f49c5601be5654bafad841de0b0d7 @ettore committed Feb 21, 2014
Showing with 41 additions and 10 deletions.
  1. +30 −9 categories/CLCGNSArrayCategory.h
  2. +11 −1 categories/CLCGNSArrayCategory.m
@@ -31,22 +31,43 @@
@interface NSArray (Candygirl)
-/**
- * @return A new array where each element is the result of calling `block`
- * on every item of the original array.
+/*!
+ @return A new array where each element is the result of calling `block`
+ on every item of the original array.
*/
-(NSArray*)mapBlock:(id(^)(id item))block;
-/**
- * Executes `item_method` on every item of the array.
- * Note: `item_method` must return an object, not a scalar value.
- *
- * @return A new array where each element is the result of calling `item_method`
- * on every item of the original array.
+/*!
+ @discussion
+ Executes `item_method` on every item of the array.
+ Note: `item_method` must return an object, not a scalar value.
+
+ @return A new array where each element is the result of calling `item_method`
+ on every item of the original array.
*/
-(NSArray*)map:(SEL)item_method;
+/*!
+ @discussion
+ Combines (i.e. reduces, folds) all elements of the receiving array
+ into an accumulated result by running the given block on each element.
+
+ Note: The return type of `block`, the type of `current_accumulator` and
+ the type of `initial_accumulator` must match.
+
+ @param block The function that will be run on the current value of the
+ accumulator using a given item of the array.
+ @param initial_accumulator_value This is the initial value that will be passed
+ to the block the first time it is executed.
+
+ @return The final value of the accumulator
+ */
+-(id)reduceWithAccumulator:(id)initial_accumulator
+ block:(id(^)(id current_accumulator, id item))block;
+
+
+
@end
@@ -44,7 +44,6 @@ -(NSArray*)mapBlock:(id(^)(id item))block
}
return a;
-
}
@@ -60,4 +59,15 @@ -(NSArray*)map:(SEL)item_method
}
+-(id)reduceWithAccumulator:(id)accumulator
+ block:(id(^)(id current_accumulator, id item))block
+{
+ for (id item in self) {
+ accumulator = block(accumulator, item);
+ }
+
+ return accumulator;
+}
+
+
@end

0 comments on commit 2620c01

Please sign in to comment.