Create a gist now

Instantly share code, notes, and snippets.

NSArray *buckets = @[@[@(1), @(2)], @[@(2)], @[@(3)]];

NSArray *objectsInBuckets = [buckets unionOfArrays];
NSLog(@"%@", objectsInBuckets);

Outputs:

(
1,
2,
2,
3
)

NSLog(@"%@", objectsInBuckets);
NSLog(@"Sum: %@", [objectsInBuckets sumOfObjects]);
NSLog(@"Average: %@", [objectsInBuckets averageOfObjects]);
NSLog(@"Min: %@", [objectsInBuckets mininumObject]);
NSLog(@"Max: %@", [objectsInBuckets maximumObject]);

Outputs:

Sum: 8
Average: 2
Min: 1
Max: 3

NSArray *distinctObjectsInBuckets = [buckets distinctUnionOfArrays];
NSLog(@"%@", distinctObjectsInBuckets);

Outputs:

(
3,
2,
1
)

NSArray *people = (@[
                   [Person personWithName:@"Jarod"],
                   [Person personWithName:@"Ben"],
                   [Person personWithName:@"Mac"],
                   [Person personWithName:@"Beamer"],
                   [Person personWithName:@"Babs"]
                   ]);
NSArray *names = [people unionOfObjectsUsingProperty:@selector(name)];
NSLog(@"%@", names);

Outputs:

(
Jarod,
Ben,
Mac,
Beamer,
Babs
)

[people maximumObjectUsingProperty:@selector(name)];

Outputs:

Mac

[people mininumObjectUsingProperty:@selector(name)];

Outputs:

Babs

NSArray *uppercaseNames = [names objectsByMappingBlock:^id(id obj) {
    return [obj uppercaseString];
}];

Outputs:

(
JAROD,
BEN,
MAC,
BEAMER,
BABS
)
//
// NSArray+CollectionOperations.h
// MSAppKit
//
// Created by Jarod Luebbert on 1/4/13.
//
//
#import <Foundation/Foundation.h>
@interface NSArray (CollectionOperations)
#pragma mark -
/*
@description: Flattens an array of arrays into a single array.
ex: @[[@(1)], [@(2)], [@(3)]] => @[@(1), @(2), @(3)]
*/
- (NSArray *)unionOfArrays;
/*
@description: Like `unionOfArrays` but removes duplicate objects.
*/
- (NSArray *)distinctUnionOfArrays;
/*
@description: Returns an array of properties of the objects in this array.
*/
- (NSArray *)unionOfObjectsUsingProperty:(SEL)property;
#pragma mark -
- (NSNumber *)sumOfObjects;
- (NSNumber *)sumOfObjectsUsingProperty:(SEL)property;
- (id)maximumObject;
- (id)maximumObjectUsingProperty:(SEL)property;
- (id)mininumObject;
- (id)mininumObjectUsingProperty:(SEL)property;
- (NSNumber *)averageOfObjects;
- (NSNumber *)averageOfObjectsUsingProperty:(SEL)property;
#pragma mark -
/*
@description: Calls the block on every object and stores the returned value
in a new array.
*/
- (NSArray *)objectsByMappingBlock:(id (^)(id))block;
@end
//
// NSArray+CollectionOperations.m
// MSAppKit
//
// Created by Jarod Luebbert on 1/4/13.
//
//
#import "NSArray+CollectionOperations.h"
#define kUnionOperationObjects @"unionOfObjects"
#define kUnionOperationArrays @"unionOfArrays"
#define kDistinctUnionOperationArrays @"distinctUnionOfArrays"
#define kSelfKeyPath @"self"
#define kSumOperation @"sum"
#define kMinOperation @"min"
#define kMaxOperation @"max"
#define kAverageOperation @"avg"
@implementation NSArray (CollectionOperations)
#pragma mark -
- (id)KVCCollectionOperation:(NSString *)operator
{
return [self KVCCollectionOperation:operator keyPath:kSelfKeyPath];
}
- (id)KVCCollectionOperation:(NSString *)operator property:(SEL)selector
{
return [self KVCCollectionOperation:operator keyPath:NSStringFromSelector(selector)];
}
- (id)KVCCollectionOperation:(NSString *)operator keyPath:(NSString *)keyPath
{
NSString *path = [NSString stringWithFormat:@"@%@.%@", operator, keyPath];
return [self valueForKeyPath:path];
}
#pragma mark -
- (NSArray *)unionOfArrays
{
return [self KVCCollectionOperation:kUnionOperationArrays];
}
- (NSArray *)distinctUnionOfArrays
{
return [self KVCCollectionOperation:kDistinctUnionOperationArrays];
}
- (NSArray *)unionOfObjectsUsingProperty:(SEL)property
{
NSString *propertyName = NSStringFromSelector(property);
return [self KVCCollectionOperation:kUnionOperationObjects keyPath:propertyName];
}
#pragma mark -
- (NSNumber *)sumOfObjects
{
return [self KVCCollectionOperation:kSumOperation];
}
- (NSNumber *)sumOfObjectsUsingProperty:(SEL)property
{
return [self KVCCollectionOperation:kSumOperation property:property];
}
- (id)maximumObject
{
return [self KVCCollectionOperation:kMaxOperation];
}
- (id)maximumObjectUsingProperty:(SEL)property
{
return [self KVCCollectionOperation:kMaxOperation property:property];
}
- (id)mininumObject
{
return [self KVCCollectionOperation:kMinOperation];
}
- (id)mininumObjectUsingProperty:(SEL)property
{
return [self KVCCollectionOperation:kMinOperation property:property];
}
- (NSNumber *)averageOfObjects
{
return [self KVCCollectionOperation:kAverageOperation];
}
- (NSNumber *)averageOfObjectsUsingProperty:(SEL)property
{
return [self KVCCollectionOperation:kAverageOperation property:property];
}
#pragma mark -
- (NSArray *)objectsByMappingBlock:(id (^)(id))block
{
NSMutableArray *items = [NSMutableArray array];
for (id obj in self)
{
[items addObject:block(obj)];
}
return items;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment