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 valueAlias: forEach
eachWithStop
Section titled “eachWithStop”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, 3Produces 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
reduce
Section titled “reduce”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));// => 6Aliases: inject, foldl
reduceRight
Section titled “reduceRight”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;});// => 2Alias: detect
filter
Section titled “filter”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
reject
Section titled “reject”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;});// => YESAlias: 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;});// => YESAlias: some
include
Section titled “include”Returns YES if the value is present in the list.
B found = _.include(AI(1, 2, 3), N.I(3));// => YESAlias: contains
invoke
Section titled “invoke”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));// => 3Returns the minimum value in a list.
N* min = _.min(AI(1, 2, 3));// => 1sortBy
Section titled “sortBy”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]groupBy
Section titled “groupBy”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]}sortedIndex
Section titled “sortedIndex”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));// => 3shuffle
Section titled “shuffle”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)toArray
Section titled “toArray”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