<?php
/*** Supply between 1..n functions each with an arity of 1 (that is, accepts* one and only one argument). Versions prior to php 5.6 do not have the* variadic operator `...` and as such require the use of `func_get_args()` to* obtain the comma-delimited list of expressions provided via the argument* list on function call.** Example - Call the function `pipe()` like:** pipe ($addOne, $multiplyByTwo);** @return closure*/function pipe(){$functions = func_get_args(); // an array of callable functions [$addOne, $multiplyByTwo]return function ($initialAccumulator) use ($functions) { // return a function with an arity of 1return array_reduce( // chain the supplied `$arg` value through each function in the list of functions$functions, // an array of functions to reduce over the supplied `$arg` valuefunction ($accumulator, $currFn) { // the reducer (a reducing function)return $currFn($accumulator);},$initialAccumulator);};}
/*** @param string $valueToFilter** @return \Closure A function that expects a 1d array and returns an array* filtered of values matching $valueToFilter.*/function filterValue($valueToFilter){return function($xs) use ($valueToFilter) {return array_filter($xs, function($x) use ($valueToFilter) {return $x !== $valueToFilter;});};}
$filterer = pipe(filterValue(null),filterValue(false),filterValue("0"),filterValue(""));
$xs = [0, 1, 2, 3, null, false, "0", ""];$xs = $filterer($xs);
echo "<pre>";var_export($xs); //=> [0, 1, 2, 3]