Skip to content

Arrays

Functions for working with arrays (NSArray).

Returns the first element(s) of an array.

N* first = _.first(AI(5, 4, 3, 2, 1));
// => 5
A* firstThree = _.first(AI(5, 4, 3, 2, 1), 3);
// => [5, 4, 3]

Alias: head, take

Returns everything but the last entry.

A* result = _.initial(AI(5, 4, 3, 2, 1));
// => [5, 4, 3, 2]

Returns the last element(s) of an array.

N* last = _.last(AI(5, 4, 3, 2, 1));
// => 1
A* lastThree = _.last(AI(5, 4, 3, 2, 1), 3);
// => [3, 2, 1]

Returns everything but the first entry.

A* result = _.rest(AI(5, 4, 3, 2, 1));
// => [4, 3, 2, 1]

Alias: tail, drop

Returns a copy with all falsy values removed.

A* result = _.compact(AA(@[@0, @1, @NO, @2, @"", @3]));
// => [1, 2, 3]

Flattens a nested array.

A* result = _.flatten(AA(@[@[@1, @2], @[@3, @[@4]]]));
// => [1, 2, 3, 4]

Returns a copy without the specified values.

A* result = _.without(AI(1, 2, 1, 0, 3, 1, 4), N.I(0), N.I(1));
// => [2, 3, 4]

Returns the union of arrays (unique values).

A* result = _.union(AI(1, 2, 3), AI(101, 2, 1, 10), AI(2, 1));
// => [1, 2, 3, 101, 10]

Returns values present in all arrays.

A* result = _.intersection(AI(1, 2, 3), AI(101, 2, 1, 10), AI(2, 1));
// => [1, 2]

Returns values from the first array not in the others.

A* result = _.difference(AI(1, 2, 3, 4, 5), AI(5, 2, 10));
// => [1, 3, 4]

Returns a duplicate-free version.

A* result = _.uniq(AI(1, 2, 1, 3, 1, 4));
// => [1, 2, 3, 4]

Alias: unique

Merges arrays by index position.

A* result = _.zip(AA(@[@"moe", @"larry", @"curly"]),
AI(30, 40, 50),
AA(@[@YES, @NO, @NO]));
// => [["moe", 30, YES], ["larry", 40, NO], ["curly", 50, NO]]

Creates an object from arrays of keys and values.

O* result = _.zipObject(AA(@[@"moe", @"larry", @"curly"]), AI(30, 40, 50));
// => {moe: 30, larry: 40, curly: 50}

Returns the index of the first occurrence.

I index = _.indexOf(AI(1, 2, 3), N.I(2));
// => 1

Returns the index of the last occurrence.

I index = _.lastIndexOf(AI(1, 2, 3, 1, 2, 3), N.I(2));
// => 4

Generates a range of integers.

A* result = _.range(10);
// => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
A* result = _.range(1, 11);
// => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
A* result = _.range(0, 30, 5);
// => [0, 5, 10, 15, 20, 25]