コード例 #1
0
ファイル: MaybeTTest.php プロジェクト: tmciver/functional-php
 public function testFlatMapForRightNothing()
 {
     // create a MaybeT that represents an `Either Maybe`
     $mt = new MaybeT(Either::fromValue(Maybe::nothing()));
     $newMt = $mt->flatMap('firstLetter');
     $this->assertEquals($newMt, $mt);
 }
コード例 #2
0
 public function testEitherToMaybeForLeft()
 {
     $left = Either::left("I am Error!");
     $conversionVisitor = new EitherToMaybe();
     $actualMaybe = $left->accept($conversionVisitor);
     $expectedMaybe = Maybe::nothing();
     $this->assertEquals($expectedMaybe, $actualMaybe);
 }
コード例 #3
0
 public function testMapForNothing()
 {
     $maybeInt = Maybe::nothing();
     $maybeIntPlusOne = $maybeInt->map(function ($i) {
         return addOne($i);
     });
     $this->assertInstanceOf('TMciver\\Functional\\Maybe\\Nothing', $maybeIntPlusOne);
 }
コード例 #4
0
ファイル: MaybeTest.php プロジェクト: tmciver/functional-php
 public function testOrElseForNothing()
 {
     $maybe = Maybe::nothing('');
     $maybeNoException = $maybe->orElse(function () {
         throw new \Exception('I should be caught!');
     }, []);
     $this->assertInstanceOf(Nothing::class, $maybeNoException);
 }
コード例 #5
0
 public function testMaybeToEitherForNothing()
 {
     $nothing = Maybe::nothing("I am Error!");
     $conversionVisitor = new MaybeToEither("Hey, I was Nothing but now I'm Left!");
     $actualEither = $nothing->accept($conversionVisitor);
     $expectedEither = Either::left("Hey, I was Nothing but now I'm Left!");
     $this->assertEquals($expectedEither, $actualEither);
 }
コード例 #6
0
 public function testApplicativeForNothing()
 {
     // The function
     $maybeFn = Maybe::nothing('There is no function!');
     // The args
     $maybeStr = Maybe::fromValue('Hello world!');
     $maybeStart = Maybe::fromValue(6);
     $maybeLength = Maybe::fromValue(5);
     // Apply the function applicatively
     $maybeResult = $maybeFn($maybeStr, $maybeStart, $maybeLength);
     $expectedResult = $maybeFn;
     $this->assertEquals($expectedResult, $maybeResult);
 }
コード例 #7
0
ファイル: HeadTest.php プロジェクト: tmciver/functional-php
 private function head($array)
 {
     if (is_array($array)) {
         if (count($array) > 0) {
             $vals = array_values($array);
             $h = Maybe::fromValue($vals[0]);
         } else {
             $h = Maybe::nothing();
         }
     } else {
         $h = Maybe::nothing();
     }
     return $h;
 }
コード例 #8
0
ファイル: MaybeT.php プロジェクト: tmciver/functional-php
 public function fail($msg)
 {
     return new MaybeT($this->monad->pure(Maybe::nothing()));
 }
コード例 #9
0
ファイル: Maybe.php プロジェクト: tmciver/functional-php
    /**
     * Function for doing double dispatch from Just::append.
     */
    protected abstract function appendJust($just);
    /**
     * @param $default The default value to return if this Maybe is an instance
     * of Nothing.
     * @return The contained value if this Maybe is an instance of Just, or the
     * default value otherwise.
     */
    public abstract function getOrElse($default);
    /**
     * Calls the Callable $f (or not) in a sub-class-specific way.
     * @param Callable $f the Callable to (optionally) call. This callable
     * should be a function of a number of arguments equal to the number of
     * array elements given in $args and should return a sub-class of Maybe.
     * @param array $args The arguments to pass to the Callable $f. The array
     * length must be equal to the number of arguments expected by $f.
     * @return An instance of Maybe.
     */
    abstract function orElse(callable $f, array $args);
    /**
     * @return True if this object is an instance of the Nothing class; false
     * otherwise.
     */
    abstract function isNothing();
    abstract function accept($maybeVisitor);
}
// initialize Maybe::$nothing
Maybe::$nothing = new Nothing();
コード例 #10
0
 function visitLeft($left)
 {
     return Maybe::nothing();
 }