Skip to content

Collections

These functions work on both arrays (NSArray) and dictionaries (NSDictionary).

Iterates over a list of elements, yielding each in turn to an iterator function.

_.each(AI(1, 2, 3), ^(N* num, ...) {
NSLog(@"%@", num);
});
// => logs each number in turn
_.each(OKV({@"one", N.I(1)}, {@"two", N.I(2)}), ^(N* num, ...) {
NSLog(@"%@", num);
});
// => logs each value

Alias: forEach

Like each, but stops iteration when the block returns NO.

_.eachWithStop(AI(1, 2, 3, 4, 5), ^B(N* num, ...) {
NSLog(@"%@", num);
return num.I < 3; // stop when num >= 3
});
// => logs 1, 2, 3

Produces a new array by mapping each value through a transformation function.

A* result = _.map(AI(1, 2, 3), ^id(N* num, ...) {
return N.I(num.I * 3);
});
// => [3, 6, 9]

Alias: collect

Reduces a list to a single value by iteratively combining elements.

N* sum = _.reduce(AI(1, 2, 3), ^id(N* memo, N* num, ...) {
return N.I(memo.I + num.I);
}, N.I(0));
// => 6

Aliases: inject, foldl

Like reduce, but iterates from right to left.

A* list = AA(@[@"a", @"b", @"c"]);
S* result = _.reduceRight(list, ^id(S* memo, S* str, ...) {
return [memo stringByAppendingString:str];
}, @"");
// => "cba"

Alias: foldr

Returns the first value that passes a truth test.

N* result = _.find(AI(1, 2, 3, 4), ^B(N* num, ...) {
return num.I % 2 == 0;
});
// => 2

Alias: detect

Returns all values that pass a truth test.

A* evens = _.filter(AI(1, 2, 3, 4, 5, 6), ^B(N* num, ...) {
return num.I % 2 == 0;
});
// => [2, 4, 6]

Alias: select

Returns all values that fail a truth test (opposite of filter).

A* odds = _.reject(AI(1, 2, 3, 4, 5, 6), ^B(N* num, ...) {
return num.I % 2 == 0;
});
// => [1, 3, 5]

Returns YES if all values pass a truth test.

B allEven = _.all(AI(2, 4, 6), ^B(N* num, ...) {
return num.I % 2 == 0;
});
// => YES

Alias: every

Returns YES if any value passes a truth test.

B hasEven = _.any(AI(1, 2, 3), ^B(N* num, ...) {
return num.I % 2 == 0;
});
// => YES

Alias: some

Returns YES if the value is present in the list.

B found = _.include(AI(1, 2, 3), N.I(3));
// => YES

Alias: contains

Calls a method on each element.

A* result = _.invoke(AA(@[@[@5, @1, @7], @[@3, @2, @1]]), @"sort");
// => [[1, 5, 7], [1, 2, 3]]

Extracts a list of property values.

A* names = _.pluck(stooges, @"name");
// => ["moe", "larry", "curly"]

Returns the maximum value in a list.

N* max = _.max(AI(1, 2, 3));
// => 3

Returns the minimum value in a list.

N* min = _.min(AI(1, 2, 3));
// => 1

Returns a sorted copy of the list, ranked by the iterator results.

A* sorted = _.sortBy(AI(1, 2, 3, 4, 5, 6), ^id(N* num, ...) {
return N.I(num.I % 3);
});
// => [3, 6, 1, 4, 2, 5]

Groups values by the result of the iterator.

O* grouped = _.groupBy(AI(1, 2, 3, 4, 5), ^id(N* num, ...) {
return num.I % 2 == 0 ? @"even" : @"odd";
});
// => {even: [2, 4], odd: [1, 3, 5]}

Determines the index at which a value should be inserted to maintain sorted order.

I index = _.sortedIndex(AI(10, 20, 30, 40, 50), N.I(35));
// => 3

Returns a randomized copy of the list.

A* shuffled = _.shuffle(AI(1, 2, 3, 4, 5, 6));
// => [4, 1, 6, 3, 5, 2] (random order)

Converts a collection to an array.

A* result = _.toArray(OKV({@"a", N.I(1)}, {@"b", N.I(2)}));
// => [1, 2]

Returns the number of elements in the list.

I count = _.size(OKV({@"one", N.I(1)}, {@"two", N.I(2)}));
// => 2