Skip to content
Ihor Burlachenko edited this page May 19, 2016 · 2 revisions

Provides missing array functions which also can be applied to traversable sequences


all($sequence, $predicate)

Returns true if all $sequence items satisfy the predicate are true (or if the $sequence is empty). If predicate was not passed return true if all $sequence items are true.

assert(true === all([true, true, true]));
any($sequence, $predicate)

Returns true if any $sequence items satisfies the predicate. If predicate was not passed returns true if any $sequence item is true. If the $sequence is empty, returns false.

assert(true === any([true, false, false]));
map($function, $sequence)

Applies function of one argument to each sequence item

assert(['A', 'B', 'C'] === map('strtoupper', ['a', 'b', 'c']));
flatMap($function, $sequence)

Applies function of one argument to each sequence item and flattens the result

$duplicate = function($v) { return [$v, $v]; }
assert(['hello', 'hello', 'world', 'world'] === flatMap($duplicate, ['hello', 'world']));
zip($sequence1, $sequence2)

Zips two or more sequences

assert([[1, 'a'], [2, 'b'], [3, 'c']] === zip([1, 2, 3], ['a', 'b', 'c']));
zipWith($function, $sequence1, $sequence2)

Generalises zip by zipping with the function given as the first argument, instead of an array-creating function

use const \nspl\op\sum;

assert([101, 1002, 10003] === zipWith(sum, [1, 2, 3], [100, 1000, 10000]));
reduce($function, $sequence, $initial = 0)

Applies function of two arguments cumulatively to the sequence items, from left to right to reduce the sequence to a single value

assert(6 === reduce(function($a, $b) { return $a + $b; }, [1, 2, 3]));

// Which is the same as
use const \nspl\op\sum;
assert(6 === reduce(sum, [1, 2, 3]));
filter($predicate, $sequence)

Returns sequence items that satisfy the predicate

assert([1, 2, 3] === filter('is_numeric', ['a', 1, 'b', 2, 'c', 3]));
filterNot($predicate, $sequence)

Returns sequence items that don't satisfy the predicate

assert(['a', 'b', 'c'] === filter('is_numeric', ['a', 1, 'b', 2, 'c', 3]));
take($sequence, $N, $step = 1)

Returns first N sequence items with given step

assert([1, 3, 5] === take([1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 2));
takeKeys($sequence, array $keys)

Returns array containing only given sequence keys

assert(array('hello' => 1, 'world' => 2) === takeKeys(array('hello' => 1, 'world' => 2, 'foo' => 3), ['hello', 'world']));
takeWhile($predicate, $sequence)

Returns the longest sequence prefix of all items which satisfy the predicate

assert([1, 2, 3] === takeWhile('is_numeric', [1, 2, 3, 'a', 'b', 'c', 4, 5, 6]));
first($sequence)

Returns the first sequence item

assert(1 === first([1, 2, 3, 4, 5, 6, 7, 8, 9]));
second($sequence)

Returns the second sequence item

assert(2 === second([1, 2, 3, 4, 5, 6, 7, 8, 9]));
drop($sequence, $N)

Drops first N sequence items

assert([7, 8, 9] === drop([1, 2, 3, 4, 5, 6, 7, 8, 9], 6));
dropWhile($predicate, $sequence)

Drops the longest sequence prefix of all items which satisfy the predicate

assert(['a', 'b', 'c', 4, 5, 6] === dropWhile('is_numeric', [1, 2, 3, 'a', 'b', 'c', 4, 5, 6]));
last($sequence)

Returns the last sequence item

assert(9 === last([1, 2, 3, 4, 5, 6, 7, 8, 9]));
partition($predicate, $sequence)

Returns two lists, one containing values for which the predicate returned true, and the other containing the items that returned false

assert([[1, 2, 3], ['a', 'b', 'c']] === partition('is_numeric', ['a', 1, 'b', 2, 'c', 3]));
span($predicate, $sequence)

Returns two lists, one containing values for which your predicate returned true until the predicate returned false, and the other containing all the items that left

assert([[1], ['a', 2, 'b', 3, 'c']] === span('is_numeric', [1, 'a', 2, 'b', 3, 'c']));
indexed($sequence, $by, $keepLast = true, $transform = null)

Returns array which contains indexed sequence items

$by is an array key or a function
If $keepLast is true only the last item with the key will be returned otherwise list of items which share the same key value will be returned
$transform is a function that transforms list item after indexing

$indexedById = indexed([
    array('id' => 1, 'name' => 'John'),
    array('id' => 2, 'name' => 'Kate'),
    array('id' => 3, 'name' => 'Robert'),
], 'id');
sorted($sequence, $reversed = false, $key = null, $cmp = null)

Returns array which contains sorted items from the passed sequence

If $reversed is true then return reversed sorted sequence. If $reversed is not boolean and $key was not passed then acts as a $key parameter
$key is a function of one argument that is used to extract a comparison key from each item
$cmp is a function of two arguments which returns a negative number, zero or positive number depending on whether the first argument is smaller than, equal to, or larger than the second argument

assert([1, 2, 3] === sorted([2, 3, 1]));
assert(['c', 'b', 'a'] === sorted(['c', 'a', 'b'], true));

$usersSortedByName = sorted($users, function($u) { return $u->getName(); });

// Which is the same as
use function nspl\op\methodCaller;
$usersSortedByName = sorted($users, methodCaller('getName'));

Check more \nspl\a\sorted examples here.

keySorted($sequence, $reversed = false)

Returns array which contains sequence items sorted by keys

assert(array('a' => 1, 'b' => 2, 'c' => 3) === keySorted(array('b' => 2, 'c' => 3, 'a' => 1));
flatten($sequence, $depth = null)

Flattens multidimensional sequence

assert([1, 2, 3, 4, 5, 6, 7, 8, 9] === flatten([[1, [2, [3]]], [[[4, 5, 6]]], 7, 8, [9]]));
assert([1, 2, [3], [4, 5, 6], 7, 8, 9] === flatten([[1, [2, [3]]], [[[4, 5, 6]]], 7, 8, [9]], 2));
pairs($sequence, $valueKey = false)

Returns list of (key, value) pairs. If $valueKey is true then convert array to (value, key) pairs.

assert([['a', 'hello'], ['b', 'world'], ['c', 42]] === pairs(array('a' => 'hello', 'b' => 'world', 'c' => 42)));
merge($sequence1, $sequence2)

Returns arrays containing $sequence1 items and $sequence2 items

assert([1, 2, 3, 4, 5, 6] === merge([1, 2, 3], [4, 5, 6]));
reorder(array $list, $from, $to)

Moves list item to another position

assert([2, 0, 1] === reorder([0, 1, 2], 2, 0)); // move item from the 2nd position to the begining of the list
value($array, $key, $default = null)

Returns array value by key if it exists otherwise returns the default value

$data = array('a' => 1, 'b' => 2, 'c' => 3);
assert(2 === value($data, 'b', -1));
assert(-1 === value($data, 'd', -1));
isList($var)

Returns true if the variable is a list

Callbacks

nspl\a provides all these functions as callbacks in its constants which have the same names as the functions.

use const \nspl\a\first;
assert([1, 2, 3] === map(first, [[1, 'a'], [2, 'b'], [3, 'c']]));

Check more \nspl\a examples here.

Clone this wiki locally