namespace PHPixme; /** * @param callable $hof * @param mixed = $startVal * @param \Traversable= $traversable * @return \Closure|mixed * @sig (Callable (a, b) -> a) -> a -> \Traversable [b] -> a */ function foldRight($hof = null, $startVal = null, $traversable = null) { return call_user_func_array(__PRIVATE__::$instance[foldRight], func_get_args()); } const foldRight = __NAMESPACE__ . '\\foldRight'; __PRIVATE__::$instance[foldRight] = __PRIVATE__::curryExactly3(function ($hof, $startVal, $arrayLike) { __CONTRACT__::argIsACallable($hof); __CONTRACT__::argIsATraversable($arrayLike, 2); if ($arrayLike instanceof FoldableInterface) { return $arrayLike->foldRight($hof, $startVal); } // Use traversableToArray, because right is non-lazy on \Traversable $array = __PRIVATE__::traversableToArray($arrayLike); $output = $startVal; end($array); while (!is_null($key = key($array))) { $output = call_user_func($hof, $output, current($array), $key, $arrayLike); prev($array); } return $output; });
<?php namespace PHPixme; // -- Starling -- /** * Starling * This combinator takes two functions and applies the third across both. * The first operand must return a function, which will relieve the result of the second. * Useful for combining mappings of the same data together * @param callable $x * @param callable $y * @param mixed $z * @return \Closure|mixed * @sig Callable x -> Callable y -> z -> a */ function S($x = null, $y = null, $z = null) { return call_user_func_array(__PRIVATE__::$instance[S], func_get_args()); } const S = __NAMESPACE__ . '\\S'; __PRIVATE__::$instance[S] = __PRIVATE__::curryExactly3(function ($x, $y, $z) { __CONTRACT__::argIsACallable($x); __CONTRACT__::argIsACallable($y, 1); return call_user_func(__CONTRACT__::composedIsACallable(call_user_func($x, $z)), call_user_func($y, $z)); });
<?php namespace PHPixme; /** * @param callable $hof * @param mixed $startVal * @param \Traversable $traversable * @return \Closure|mixed * @sig (Callable (a, b) -> a) -> a -> \Traversable [b] -> a */ function fold($hof = null, $startVal = null, $traversable = null) { return call_user_func_array(__PRIVATE__::$instance[fold], func_get_args()); } const fold = __NAMESPACE__ . '\\fold'; __PRIVATE__::$instance[fold] = __PRIVATE__::curryExactly3(function ($hof, $startVal, $arrayLike) { __CONTRACT__::argIsACallable($hof); if ($arrayLike instanceof FoldableInterface) { return $arrayLike->fold($hof, $startVal); } $output = $startVal; foreach (__PRIVATE__::protectTraversable($arrayLike) as $key => $value) { $output = call_user_func($hof, $output, $value, $key, $arrayLike); } return $output; });