Functions for working with arrays and iterables. Every function here accepts a plain array, an
Iterator, or aGenerator— lazy where possible, short-circuit where the algorithm allows, terminal only where unavoidable. Each function page carries a badge telling you which category it falls into.
use PinkCrab\FunctionConstructors\Arrays as Arr;
use PinkCrab\FunctionConstructors\Numbers as Num;
$doubleEvens = F\compose(
Arr\filter(Num\isMultipleOf(2)), // keep evens
Arr\map(Num\multiply(2)) // double them
);
$doubleEvens([1, 2, 3, 4, 5, 6]); // [4, 8, 12]The map family includes map, mapKey, mapWith, mapWithKey, flatMap, column. The filter family: filter, filterKey, filterAnd, filterOr, filterMap, plus the short-circuit variants filterFirst, filterAny and the terminal variants filterLast, filterAll, filterCount, partition.
$users = [
'ada' => ['name' => 'Ada', 'age' => 40],
'bea' => ['name' => 'Bea', 'age' => 30],
'cal' => ['name' => 'Cal', 'age' => 50],
];
$sortByAge = Arr\uasort(fn($a, $b) => $a['age'] <=> $b['age']);
$sortByAge($users);
// [
// 'bea' => ['name' => 'Bea', 'age' => 30],
// 'ada' => ['name' => 'Ada', 'age' => 40],
// 'cal' => ['name' => 'Cal', 'age' => 50],
// ]
// — keys preserved, $users unchangedEvery native PHP sort has a curried, immutable counterpart: sort, rsort, ksort, krsort, asort, arsort, natsort, natcasesort, uksort, uasort, usort.
$orders = [
['id' => 1, 'status' => 'paid', 'amount' => 50],
['id' => 2, 'status' => 'paid', 'amount' => 20],
['id' => 3, 'status' => 'refunded', 'amount' => 15],
['id' => 4, 'status' => 'paid', 'amount' => 35],
];
$byStatus = Arr\groupBy(F\getProperty('status'))($orders);
// [
// 'paid' => [ <order 1>, <order 2>, <order 4> ],
// 'refunded' => [ <order 3> ],
// ]
$totalPaid = Arr\sumWhere(F\getProperty('amount'))($byStatus['paid']);
// 105 (50 + 20 + 35)Also: fold, foldR, foldKeys, scan, scanR, partition, chunk.
take, takeLast, takeUntil, takeWhile, head, last, tail — prefix/suffix slicing and single-value access. Most of these are lazy or short-circuiting, making them safe even with infinite Generators.
append, prepend, zip, pick, replace, replaceRecursive, flattenByN, toObject, toJson, toString, arrayCompiler, arrayCompilerTyped, each.
groupBy, partition, sumWhere, fold in action.