Skip to content

Commit

Permalink
Rename block types to *Func instead of *Block
Browse files Browse the repository at this point in the history
Makes more sense with how they are being used.
  • Loading branch information
hallski committed Mar 28, 2015
1 parent 249844c commit 4f4899d
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
8 changes: 4 additions & 4 deletions Example.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ MOGTransformation Trim(NSUInteger drop, NSUInteger finalSize)
return MOGCompose(MOGDrop(drop), MOGTake(finalSize));
}

MOGMapBlock SortArrayOfNumbers(BOOL ascending)
MOGMapFunc SortArrayOfNumbers(BOOL ascending)
{
NSSortDescriptor *lowestToHighest = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:ascending];

Expand All @@ -16,14 +16,14 @@ MOGMapBlock SortArrayOfNumbers(BOOL ascending)
};
}

MOGMapBlock TrimArray(NSUInteger trimN)
MOGMapFunc TrimArray(NSUInteger trimN)
{
return ^id(NSArray *values) {
return [values mog_transduce:TrimTransducer(trimN, values.count - 2 * trimN)];
};
}

MOGMapBlock MeanOfArrayOfNumbers()
MOGMapFunc MeanOfArrayOfNumbers()
{
return ^id(NSArray *numbers) {
return [numbers valueForKeyPath:@"@avg.self"];
Expand Down Expand Up @@ -54,7 +54,7 @@ NSNumber *number = MOGTransform(array, MOGLastValueReducer(), @0, filter);
// number == 7.5

// Or we can simulate the numbers coming in from a stream and manually feed numbers to the filter.
MOGMapBlock manualFilter = MOGValueTransformer(filter);
MOGMapFunc manualFilter = MOGValueTransformer(filter);

number = manualFilter(@14); // number == 14
number = manualFilter(@13); // number == 14
Expand Down
28 changes: 14 additions & 14 deletions MogKit/MogReduce.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
/**
* Block that generates the initial value of a reduction.
*/
typedef id (^MOGReducerInititialBlock) (void);
typedef id (^MOGReducerInititialFunc) (void);

/**
* Block that is called by `MOGTransform` after a reduction is done, this to allow step functions or the
* collection `MOGReducer` to do some final manipulation of the data.
*/
typedef id (^MOGReducerCompleteBlock) (id);
typedef id (^MOGReducerCompleteFunc) (id);

/**
* A reduce block is passed to `MOGReduce` and is used to collect the accumulated value of the
Expand All @@ -25,16 +25,16 @@ typedef id (^MOGReducerCompleteBlock) (id);
*
* Set stop to YES to terminate the reduction.
*/
typedef id (^MOGReduceBlock) (id acc, id val, BOOL *stop);
typedef id (^MOGReduceFunc) (id acc, id val, BOOL *stop);

/**
* A reducer takes an accumulated value and the next value and combines them into a new accumulated value.
* The return accumulated value is typically passed in as `acc` on successive calls.
*/
@interface MOGReducer : NSObject
@property (nonatomic, copy) MOGReduceBlock reduce;
@property (nonatomic, copy) MOGReducerInititialBlock initial;
@property (nonatomic, copy) MOGReducerCompleteBlock complete;
@property (nonatomic, copy) MOGReduceFunc reduce;
@property (nonatomic, copy) MOGReducerInititialFunc initial;
@property (nonatomic, copy) MOGReducerCompleteFunc complete;

/**
* Initialize a `MOGReducer` with blocks to create initial value, completion handler and reduce block.
Expand All @@ -48,9 +48,9 @@ typedef id (^MOGReduceBlock) (id acc, id val, BOOL *stop);
*
* @return the reducer
* */
- (instancetype)initWithInitBlock:(MOGReducerInititialBlock)initBlock
completeBlock:(MOGReducerCompleteBlock)completeBlock
reduceBlock:(MOGReduceBlock)reduceBlock;
- (instancetype)initWithInitBlock:(MOGReducerInititialFunc)initBlock
completeBlock:(MOGReducerCompleteFunc)completeBlock
reduceBlock:(MOGReduceFunc)reduceBlock;

/**
* Class method to create a step reducer (used when implementing transformations).
Expand All @@ -59,7 +59,7 @@ typedef id (^MOGReduceBlock) (id acc, id val, BOOL *stop);
*
* @return a newly created step reducer.
*/
+ (instancetype)stepReducerWithNextReducer:(MOGReducer *)nextReducer reduceBlock:(MOGReduceBlock)reduceBlock;
+ (instancetype)stepReducerWithNextReducer:(MOGReducer *)nextReducer reduceBlock:(MOGReduceFunc)reduceBlock;

/**
* Class method to create a step reducer (used when implementing transformations). Use this when the transformation
Expand All @@ -73,8 +73,8 @@ typedef id (^MOGReduceBlock) (id acc, id val, BOOL *stop);
* @return a newly created step reducer.
*/
+ (instancetype)stepReducerWithNextReducer:(MOGReducer *)nextReducer
reduceBlock:(MOGReduceBlock)reduceBlock
completeBlock:(MOGReducerCompleteBlock)completeBlock;
reduceBlock:(MOGReduceFunc)reduceBlock
completeBlock:(MOGReducerCompleteFunc)completeBlock;

@end

Expand All @@ -86,7 +86,7 @@ typedef id (^MOGReduceBlock) (id acc, id val, BOOL *stop);
* @param reduceBlock called for each value in the reduction.
*
*/
MOGReducer *MOGSimpleReducer(MOGReduceBlock reduceBlock);
MOGReducer *MOGSimpleReducer(MOGReduceFunc reduceBlock);

/**
* A reducer that accumulates values in an array. If the reducer initial block isn't used to produce the
Expand Down Expand Up @@ -124,4 +124,4 @@ MOGReducer *MOGStringConcatReducer(NSString *separator);
*
* @return returns the final return value of `reduceBlock`.
*/
id MOGReduce(id<NSFastEnumeration> source, MOGReduceBlock reduceBlock, id initial);
id MOGReduce(id<NSFastEnumeration> source, MOGReduceFunc reduceBlock, id initial);
16 changes: 8 additions & 8 deletions MogKit/MogReduce.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#import "MogReduce.h"

MOGReducer *MOGSimpleReducer(MOGReduceBlock reduceBlock)
MOGReducer *MOGSimpleReducer(MOGReduceFunc reduceBlock)
{
return [[MOGReducer alloc] initWithInitBlock:^id { return nil; }
completeBlock:^id(id accumulated) { return accumulated; }
Expand Down Expand Up @@ -54,7 +54,7 @@
}];
}

id MOGReduce(id<NSFastEnumeration> source, MOGReduceBlock reduceBlock, id initial)
id MOGReduce(id<NSFastEnumeration> source, MOGReduceFunc reduceBlock, id initial)
{
id acc = initial;

Expand All @@ -71,9 +71,9 @@ id MOGReduce(id<NSFastEnumeration> source, MOGReduceBlock reduceBlock, id initia

@implementation MOGReducer

- (instancetype)initWithInitBlock:(MOGReducerInititialBlock)initBlock
completeBlock:(MOGReducerCompleteBlock)completeBlock
reduceBlock:(MOGReduceBlock)reduceBlock
- (instancetype)initWithInitBlock:(MOGReducerInititialFunc)initBlock
completeBlock:(MOGReducerCompleteFunc)completeBlock
reduceBlock:(MOGReduceFunc)reduceBlock
{
if (self = [super init]) {
self.initial = initBlock;
Expand All @@ -84,7 +84,7 @@ - (instancetype)initWithInitBlock:(MOGReducerInititialBlock)initBlock
return self;
}

+ (instancetype)stepReducerWithNextReducer:(MOGReducer *)nextReducer reduceBlock:(MOGReduceBlock)reduceBlock
+ (instancetype)stepReducerWithNextReducer:(MOGReducer *)nextReducer reduceBlock:(MOGReduceFunc)reduceBlock
{
return [self stepReducerWithNextReducer:nextReducer
reduceBlock:reduceBlock
Expand All @@ -94,8 +94,8 @@ + (instancetype)stepReducerWithNextReducer:(MOGReducer *)nextReducer reduceBlock
}

+ (instancetype)stepReducerWithNextReducer:(MOGReducer *)nextReducer
reduceBlock:(MOGReduceBlock)reduceBlock
completeBlock:(MOGReducerCompleteBlock)completeBlock
reduceBlock:(MOGReduceFunc)reduceBlock
completeBlock:(MOGReducerCompleteFunc)completeBlock
{
return [[self alloc] initWithInitBlock:^id { return nextReducer.initial(); }
completeBlock:completeBlock
Expand Down
14 changes: 7 additions & 7 deletions MogKit/MogTransformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
typedef MOGReducer *(^MOGTransformation) (MOGReducer *);

/**
* A `MOGMapBlock` is a function you typically use with `MOGMap` which is a transformation of a single value
* A `MOGMapFunc` is a function you typically use with `MOGMap` which is a transformation of a single value
* into a new value.
*/
typedef id (^MOGMapBlock) (id val);
typedef id (^MOGMapFunc) (id val);

/**
* `MOGPredicate` is a function that by examining the value returns YES or NO. It's typically used in `MOGFilter` or
Expand All @@ -44,7 +44,7 @@ MOGTransformation MOGIdentity(void);
*
* @return a transformation that applies the map transformation.
*/
MOGTransformation MOGMap(MOGMapBlock mapBlock);
MOGTransformation MOGMap(MOGMapFunc mapBlock);

/**
* Creates a transformation that filters all values based on the `predicate` function. Values where `predicate` returns
Expand Down Expand Up @@ -169,7 +169,7 @@ MOGTransformation MOGReplaceWithDefault(NSDictionary *replacements, id defaultVa
*
* @see `MOGFilter`, `MOGRemove`.
*/
MOGTransformation MOGMapDropNil(MOGMapBlock mapBlock);
MOGTransformation MOGMapDropNil(MOGMapFunc mapBlock);

/**
* Creates a transformation that drops all duplicates. Whether it's a duplicate is determined by `isEqual:`
Expand Down Expand Up @@ -208,7 +208,7 @@ MOGTransformation MOGFlatten(void);
*
* @return a transformation that applies `mapBlock` on all values and then concatenates the result.
*/
MOGTransformation MOGFlatMap(MOGMapBlock mapBlock);
MOGTransformation MOGFlatMap(MOGMapFunc mapBlock);

/**
* Creates a transformation that splits the the values into separate `NSArray`s every time the `partitioningBlock`
Expand All @@ -218,7 +218,7 @@ MOGTransformation MOGFlatMap(MOGMapBlock mapBlock);
*
* @return a stateful transformation that splits incoming values into separate partitions.
*/
MOGTransformation MOGPartitionBy(MOGMapBlock partitioningBlock);
MOGTransformation MOGPartitionBy(MOGMapFunc partitioningBlock);

/**
* Creates a transformation that splits the values into separate `NSArray`s every `size` elements. A smaller array may be
Expand Down Expand Up @@ -303,4 +303,4 @@ id MOGTransformWithInitial(id<NSFastEnumeration> source, MOGReducer *reducer, id
*
* @return a function for transforming input values.
*/
MOGMapBlock MOGValueTransformer(MOGTransformation transformation);
MOGMapFunc MOGValueTransformer(MOGTransformation transformation);
8 changes: 4 additions & 4 deletions MogKit/MogTransformation.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ MOGTransformation MOGReplaceWithDefault(NSDictionary *replacements, id defaultVa
});
}

MOGTransformation MOGMapDropNil(MOGMapBlock mapBlock) {
MOGTransformation MOGMapDropNil(MOGMapFunc mapBlock) {
return MOGCompose(MOGMap(mapBlock), MOGDropNil());
}

Expand Down Expand Up @@ -197,12 +197,12 @@ MOGTransformation MOGFlatten(void)
};
}

MOGTransformation MOGFlatMap(MOGMapBlock mapBlock)
MOGTransformation MOGFlatMap(MOGMapFunc mapBlock)
{
return MOGCompose(MOGMap(mapBlock), MOGFlatten());
}

MOGTransformation MOGPartitionBy(MOGMapBlock partitioningBlock) {
MOGTransformation MOGPartitionBy(MOGMapFunc partitioningBlock) {
return ^MOGReducer *(MOGReducer *reducer) {
__block id lastPartitionKey = nil;
__block NSMutableArray *currentPartition = [NSMutableArray new];
Expand Down Expand Up @@ -313,7 +313,7 @@ id MOGTransformWithInitial(id<NSFastEnumeration> source, MOGReducer *reducer, id
return tr.complete(MOGReduce(source, tr.reduce, initial));
}

MOGMapBlock MOGValueTransformer(MOGTransformation transformation) {
MOGMapFunc MOGValueTransformer(MOGTransformation transformation) {
MOGReducer *reducer = transformation(MOGLastValueReducer());

return ^id(id val) {
Expand Down
2 changes: 1 addition & 1 deletion MogKit/NSArray+MogKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*
* @return a newly created array with the transformed values.
*/
- (NSArray *)mog_map:(MOGMapBlock)mapBlock;
- (NSArray *)mog_map:(MOGMapFunc)mapBlock;

/**
* Convenience method for calling `-mog_transform:` with `MOGFilter`.
Expand Down
2 changes: 1 addition & 1 deletion MogKit/NSArray+MogKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ + (NSArray *)mog_transformedArrayFromEnumeration:(id<NSFastEnumeration>)enumerat
return MOGTransform(enumeration, MOGArrayReducer(), transformation);
}

- (NSArray *)mog_map:(MOGMapBlock)mapBlock
- (NSArray *)mog_map:(MOGMapFunc)mapBlock
{
return [self mog_transform:MOGMap(mapBlock)];
}
Expand Down
4 changes: 2 additions & 2 deletions MogKitTests/MogTransformationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ - (void)testComposeArrayOfTransformations

- (void)testTransformer
{
MOGMapBlock add10 = MOGValueTransformer(MOGMap(^id(NSNumber *number) {
MOGMapFunc add10 = MOGValueTransformer(MOGMap(^id(NSNumber *number) {
return @(number.intValue + 10);
}));

Expand All @@ -512,7 +512,7 @@ - (void)testTransformer

- (void)testTransformerWithTerminatedTransformation
{
MOGMapBlock add10TerminateAfter2 = MOGValueTransformer(MOGCompose(MOGTake(2), MOGMap(^id(NSNumber *number) {
MOGMapFunc add10TerminateAfter2 = MOGValueTransformer(MOGCompose(MOGTake(2), MOGMap(^id(NSNumber *number) {
return @(number.intValue + 10);
})));

Expand Down

0 comments on commit 4f4899d

Please sign in to comment.