/** * @param $f * @param $container * @return array|callable */ protected static function __fmap($f, $container) { /** * If $container is a simple array, just defer to array_map */ if (is_array($container)) { return Arrays::map($f, $container); } /** * If $container is a function, we defer to compose */ if ($container instanceof Closure) { return Lambda::compose($f, $container); } /** * If $container implements the Traversable interface, we can foreach over it */ if ($container instanceof Traversable) { $result = []; foreach ($container as $item) { $result[] = $f($item); } return $result; } /** * Otherwise we just need to defer to the instances' internal fmap definition */ return $container->fmap($f); }
public function __construct(Reflector $module) { $makeFunctionDoc = function ($function) { return FunctionDocFactory::createFunctionDocFromReflector($function); }; $this->module = $module; $this->functionDocs = Arrays::map($makeFunctionDoc, $this->getFunctions()); }
/** * Logical And Combinator * * Given n functions {f1, f2, ..., fn}, combine them in such a way to produce a new * function g that returns true given {f1(x), f2(x), ... fn(x)} all return true. * * @example * $funcF = function($x) { return $x < 5; }; * $funcG = function($x) { return $x > 0; }; * $combinator = Logic::andCombinator([$funcF, $funcG]); * $combinator(4); // True * $combinator(2); // True * $combinator(7); // False * * @type [(a -> Bool)] -> a -> Bool * * @param array $fs array of functions to combine * @param mixed $a value to test * @return \Closure test for or using provided functions */ protected static function __andCombinator(array $fs, $a) { return self::all(Arrays::map(function ($c) use($a) { return $c($a); }, $fs)); }