Skip to content
This repository was archived by the owner on Jan 25, 2019. It is now read-only.
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
1 change: 1 addition & 0 deletions Underscore/USArrayWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
@property (readonly) USArrayWrapper *(^each)(UnderscoreArrayIteratorBlock block);
@property (readonly) USArrayWrapper *(^map)(UnderscoreArrayMapBlock block);
@property (readonly) USArrayWrapper *(^zipWith)(NSArray *array, UnderscoreArrayZipWithBlock block);
@property (readonly) USArrayWrapper *zip;

@property (readonly) USArrayWrapper *(^pluck)(NSString *keyPath);

Expand Down
13 changes: 13 additions & 0 deletions Underscore/USArrayWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ - (USArrayWrapper *)shuffle
};
}

- (USArrayWrapper *)zip
{
int length = (int)[self.first count];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be using NSInteger.

NSMutableArray *finalMutableArray = [NSMutableArray arrayWithCapacity:length * self.array.count];
for (NSUInteger index = 0; index < length; index++) {
USArrayWrapper *wrapper = self.map(^id (id obj) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not:

[finalMutableArray addObject: self.map(^id (id obj) {
    return obj[index];
}).unwrap];

I don't think having it pulled apart really adds any clarity in this case. In fact I found it a bit harder to follow with the intermediate wrapper variable being used.

return [obj objectAtIndex:index];
});
[finalMutableArray addObject:wrapper.unwrap];
}
return [[USArrayWrapper alloc] initWithArray:finalMutableArray];
}

- (USArrayWrapper *(^)(NSString *))pluck
{
return ^USArrayWrapper *(NSString *keyPath) {
Expand Down
2 changes: 1 addition & 1 deletion Underscore/Underscore+Functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
+ (void (^)(NSArray *array, UnderscoreArrayIteratorBlock block))arrayEach;
+ (NSArray *(^)(NSArray *array, UnderscoreArrayMapBlock block))arrayMap;
+ (NSArray *(^)(NSArray *firstArray, NSArray *secondArray, UnderscoreArrayZipWithBlock block))arrayZipWith;

+ (NSArray *(^)(NSArray *))zip;
+ (NSArray *(^)(NSArray *array, NSString *keyPath))pluck;

+ (NSArray *(^)(NSArray *array))uniq;
Expand Down
7 changes: 7 additions & 0 deletions Underscore/Underscore+Functional.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ @implementation Underscore (Functional)
};
}

+ (NSArray *(^)(NSArray *))zip
{
return ^(NSArray *array) {
return Underscore.array(array).zip.unwrap;
};
}

+ (NSArray *(^)(NSArray *, NSString *))pluck
{
return ^(NSArray *array, NSString *keyPath) {
Expand Down