public function testEitherToMaybeForRight()
 {
     $right = Either::fromValue("Hello");
     $conversionVisitor = new EitherToMaybe();
     $actualMaybe = $right->accept($conversionVisitor);
     $expectedMaybe = Maybe::fromValue("Hello");
     $this->assertEquals($expectedMaybe, $actualMaybe);
 }
 public function testFlatMapForJust()
 {
     $maybeInt = Maybe::fromValue(1);
     $maybeIntPlusOne = $maybeInt->flatMap(function ($i) {
         return maybeAddOne($i);
     });
     $this->assertEquals($maybeIntPlusOne->get(), 2);
 }
 public function testMapNullHandling()
 {
     $maybeInt = Maybe::fromValue(1);
     $maybeIntPlusOne = $maybeInt->map(function ($i) {
         return returnNull($i);
     });
     $this->assertInstanceOf('TMciver\\Functional\\Maybe\\Nothing', $maybeIntPlusOne);
 }
Exemple #4
0
 public function testStaticMethodCall()
 {
     $maybe = Maybe::fromValue("Hello world!");
     $f = ['\\TMciver\\Functional\\Test\\StaticClass', 'toUpperCase'];
     $maybeUpperCase = $maybe->map($f);
     $expexted = Maybe::fromValue("HELLO WORLD!");
     $this->assertEquals($expexted, $maybeUpperCase);
 }
 public function testMaybeToEitherForJust()
 {
     $just = Maybe::fromValue("Hello");
     $conversionVisitor = new MaybeToEither();
     $actualEither = $just->accept($conversionVisitor);
     $expectedEither = Either::fromValue("Hello");
     $this->assertEquals($expectedEither, $actualEither);
 }
 public function testPure()
 {
     // create a MaybeT that represents an `Either Maybe`
     $mt = new MaybeT(Either::left("I am Error."));
     // create a pure value
     $newMt = $mt->pure("Hello!");
     $expectedMt = new MaybeT(Either::fromValue(Maybe::fromValue("Hello!")));
     $this->assertEquals($newMt, $expectedMt);
 }
 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);
 }
Exemple #8
0
 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;
 }
 public function testAssociativity()
 {
     // we'll combine four values in two different ways and make sure the
     // results are the same.
     $just1 = Maybe::fromValue(1);
     $just2 = Maybe::fromValue(2);
     $just3 = Maybe::fromValue(3);
     $just4 = Maybe::fromValue(4);
     // First, combine the first two, then the last two and finally combine
     // the two results.
     $firstTwo = $just1->append($just2);
     $secondTwo = $just3->append($just4);
     $result1 = $firstTwo->append($secondTwo);
     // Next, combine the first two, then combine that result with the third
     // and then combine that result with the fourth.
     $firstThree = $firstTwo->append($just3);
     $result2 = $firstThree->append($just4);
     $this->assertEquals($result1, $result2);
 }
Exemple #10
0
 public function pure($val)
 {
     return new MaybeT($this->monad->pure(Maybe::fromValue($val)));
 }
 function visitRight($right)
 {
     return Maybe::fromValue($right->get());
 }
Exemple #12
0
 protected function appendJust($just)
 {
     // this is where the real work of appending two Just's is done.
     // Since we can't know if the value contained in a Maybe is itself a
     // monoid, we're just going to put the values in an array. But there are
     // four cases that we have to account for to create the proper result
     // array so that associativity is maintained.
     $leftVal = $just->val;
     $rightVal = $this->val;
     if (!is_array($leftVal) && !is_array($rightVal)) {
         $resultArray = [$leftVal, $rightVal];
     } else {
         if (is_array($leftVal) && !is_array($rightVal)) {
             $leftVal[] = $rightVal;
             $resultArray = $leftVal;
         } else {
             if (!is_array($leftVal) && is_array($rightVal)) {
                 array_unshift($rightVal, $leftVal);
                 $resultArray = $rightVal;
             } else {
                 // both values are arrays
                 $resultArray = array_merge($leftVal, $rightVal);
             }
         }
     }
     return Maybe::fromValue($resultArray);
 }