Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Added mapWithIndex category method for NSArray #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UsefulBits/Foundation/NSArray+Blocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

- (NSArray *)map:(id (^)(id item))block;
- (NSArray *)map:(id (^)(id item))block filterNil:(BOOL)filter_nil;
- (NSArray *)mapWithIndex:(id (^)(id item, NSUInteger index))block;
- (NSArray *)mapWithIndex:(id (^)(id item, NSUInteger index))block filterNil:(BOOL)filter_nil;
- (id)reduce:(id (^)(id current, id item))block initial:(id)initial;
- (NSArray *)intersperse:(id (^) (id current, id next))separator;

Expand Down
23 changes: 18 additions & 5 deletions UsefulBits/Foundation/NSArray+Blocks.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,26 @@ - (NSArray *)map:(id (^)(id item))block;
}

- (NSArray *)map:(id (^)(id item))block filterNil:(BOOL)filter_nil;
{
return [self mapWithIndex:^id(id item, NSUInteger index) {
return block(item);
} filterNil:filter_nil];
}

- (NSArray *)mapWithIndex:(id (^)(id item, NSUInteger index))block;
{
return [self mapWithIndex:block filterNil:YES];
}

- (NSArray *)mapWithIndex:(id (^)(id item, NSUInteger index))block filterNil:(BOOL)filter_nil;
{
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]];
for (id obj in self)

for (NSUInteger idx = 0; idx < [self count]; idx++)
{
id instance = block(obj);

id obj = [self objectAtIndex:idx];
id instance = block(obj, idx);

if (nil != instance)
{
[result addObject:instance];
Expand All @@ -169,7 +182,7 @@ - (NSArray *)map:(id (^)(id item))block filterNil:(BOOL)filter_nil;
[result addObject:[NSNull null]];
}
}

return [NSArray arrayWithArray:result];
}

Expand Down