Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

106 lines (88 sloc) 2.13 kb
//
// MACollectionUtilities.m
// MACollectionUtilities
//
// Created by Michael Ash on 10/11/10.
// Copyright 2010 Michael Ash. All rights reserved.
//
#import "MACollectionUtilities.h"
@implementation NSArray (MACollectionUtilities)
- (NSArray *)ma_map: (id (^)(id obj))block
{
NSMutableArray *array = [NSMutableArray arrayWithCapacity: [self count]];
for(id obj in self)
[array addObject: block(obj)];
return array;
}
- (NSArray *)ma_select: (BOOL (^)(id obj))block
{
NSMutableArray *array = [NSMutableArray array];
for(id obj in self)
if(block(obj))
[array addObject: obj];
return array;
}
- (id)ma_match: (BOOL (^)(id obj))block
{
for(id obj in self)
if(block(obj))
return obj;
return nil;
}
- (id)ma_reduce: (id)initial block: (id (^)(id a, id b))block
{
id a = initial;
for(id b in self)
a = block(a, b);
return a;
}
- (NSArray *)ma_sorted: (BOOL (^)(id a, id b))lessThan
{
return [self sortedArrayUsingComparator: ^NSComparisonResult(id a, id b) {
if(lessThan(a, b))
return NSOrderedAscending;
else if(lessThan(b, a))
return NSOrderedDescending;
else
return NSOrderedSame;
}];
}
- (void)ma_do: (void (^)(id obj))block
{
for(id obj in self)
block(obj);
}
@end
@implementation NSSet (MACollectionUtilities)
- (NSSet *)ma_map: (id (^)(id obj))block
{
NSMutableSet *set = [NSMutableSet setWithCapacity: [self count]];
for(id obj in self)
[set addObject: block(obj)];
return set;
}
- (NSSet *)ma_select: (BOOL (^)(id obj))block
{
NSMutableSet *set = [NSMutableSet set];
for(id obj in self)
if(block(obj))
[set addObject: obj];
return set;
}
- (id)ma_match: (BOOL (^)(id obj))block
{
for(id obj in self)
if(block(obj))
return obj;
return nil;
}
- (NSArray *)ma_sorted: (BOOL (^)(id a, id b))lessThan
{
return [[self allObjects] ma_sorted: lessThan];
}
- (void)ma_do: (void (^)(id obj))block
{
for(id obj in self)
block(obj);
}
@end
Jump to Line
Something went wrong with that request. Please try again.