getNumberOfRequiredParameters() : $reflection->getNumberOfParameters(); return curryN( $count, $fn ); } /** * @param string $fnName * * @return \Closure */ function apply( $fnName ) { $args = array_slice( func_get_args(), 1 ); return function ( $container ) use ( $fnName, $args ) { return call_user_func_array( [ $container, $fnName ], $args ); }; } /** * Returns an Invoker that runs the member function. Use `with` to set the arguments * of the member function and then invoke with `()` * * eg. give Test class: * class Test { * * private $times; * * public function __construct( $times ) { * $this->times = $times; * } * * public function multiply( $x ) { * return $x * $this->times; * } * } * * $invoker = invoke( 'multiply' )->with( 10 ); * $result = $invoker( new Test( 2 ) ); // 20 * * * @param string $fnName * * @return _Invoker */ function invoke( $fnName ) { return new _Invoker( $fnName ); } /** * @param callable $fn * * @return \Closure */ function chain( callable $fn ) { return function ( $container ) use ( $fn ) { if ( method_exists( $container, 'chain' ) ) { return $container->chain( $fn ); } elseif ( method_exists( $container, 'flatMap' ) ) { return $container->flatMap( $fn ); } elseif ( method_exists( $container, 'join' ) ) { return $container->map( $fn )->join(); } else { throw new \Exception( 'chainable method not found' ); } }; } /** * @param callable $fn * * @return \Closure */ function flatMap( callable $fn ) { return chain( $fn ); } /** * @param callable $fn * * @return Either */ function tryCatch( callable $fn ) { try { return Right::of( $fn() ); } catch ( \Exception $e ) { return Either::left( $e ); } } /** * @param callable $fn * * @return \Closure */ function flip( callable $fn ) { return function () use ( $fn ) { $args = func_get_args(); if ( count( $args ) > 1 ) { $temp = $args[0]; $args[0] = $args[1]; $args[1] = $temp; } return call_user_func_array( $fn, $args ); }; }