-
Notifications
You must be signed in to change notification settings - Fork 16
nspl\a
Provides missing array functions which also can be applied to traversable sequences
- all
- any
- map
- flatMap
- zip
- zipWith
- reduce
- filter
- filterNot
- take
- takeKeys
- takeWhile
- first
- second
- drop
- dropWhile
- last
- partition
- span
- indexed
- sorted
- keySorted
- flatten
- pairs
- merge
- reorder
- value
- isList
- Callbacks
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]));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]));Applies function of one argument to each sequence item
assert(['A', 'B', 'C'] === map('strtoupper', ['a', 'b', 'c']));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']));Zips two or more sequences
assert([[1, 'a'], [2, 'b'], [3, 'c']] === zip([1, 2, 3], ['a', 'b', 'c']));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]));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]));Returns sequence items that satisfy the predicate
assert([1, 2, 3] === filter('is_numeric', ['a', 1, 'b', 2, 'c', 3]));Returns sequence items that don't satisfy the predicate
assert(['a', 'b', 'c'] === filter('is_numeric', ['a', 1, 'b', 2, 'c', 3]));Returns first N sequence items with given step
assert([1, 3, 5] === take([1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 2));Returns array containing only given sequence keys
assert(array('hello' => 1, 'world' => 2) === takeKeys(array('hello' => 1, 'world' => 2, 'foo' => 3), ['hello', 'world']));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]));Returns the first sequence item
assert(1 === first([1, 2, 3, 4, 5, 6, 7, 8, 9]));Returns the second sequence item
assert(2 === second([1, 2, 3, 4, 5, 6, 7, 8, 9]));Drops first N sequence items
assert([7, 8, 9] === drop([1, 2, 3, 4, 5, 6, 7, 8, 9], 6));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]));Returns the last sequence item
assert(9 === last([1, 2, 3, 4, 5, 6, 7, 8, 9]));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]));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']));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');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.
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));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));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)));Returns arrays containing $sequence1 items and $sequence2 items
assert([1, 2, 3, 4, 5, 6] === merge([1, 2, 3], [4, 5, 6]));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 listReturns 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));Returns true if the variable is a list
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.