Beispiel #1
0
 public function testEitherErrorMessage()
 {
     $errMsg = 'No power in the \'verse can stop me.';
     $either = Either::fromValue(null, $errMsg);
     $expected = Either::left($errMsg);
     $this->assertEquals($expected, $either);
 }
 public function testFlatMapForLeft()
 {
     $eitherInt = Either::left("Error!");
     $eitherIntPlusOne = $eitherInt->flatMap(function ($i) {
         return Either::fromValue($i + 1);
     });
     $this->assertInstanceOf('TMciver\\Functional\\Either\\Left', $eitherIntPlusOne);
 }
 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);
 }
 public function testEitherToMaybeForLeft()
 {
     $left = Either::left("I am Error!");
     $conversionVisitor = new EitherToMaybe();
     $actualMaybe = $left->accept($conversionVisitor);
     $expectedMaybe = Maybe::nothing();
     $this->assertEquals($expectedMaybe, $actualMaybe);
 }
 public function testMapLeft()
 {
     $toUpper = function ($str) {
         return strtoupper($str);
     };
     $eitherStr = Either::left("We have a problem!");
     $mappedEitherStr = $eitherStr->map($toUpper);
     $this->assertInstanceOf('TMciver\\Functional\\Either\\Left', $mappedEitherStr);
 }
Beispiel #6
0
 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);
 }
Beispiel #7
0
 public function testTraverseForReturningNull()
 {
     $intsArray = new AssociativeArray([2, 0, 6]);
     $instance = Either::left('');
     $eitherResults = $intsArray->traverse(function ($ignore) {
         return null;
     }, $instance);
     $expected = $instance->fail();
     $this->assertInstanceOf(TMciver\Functional\Either\Left::class, $expected);
 }
Beispiel #8
0
 public function orElse(callable $f, array $args)
 {
     // Since we don't know if $f will throw an exception, we wrap the call
     // in a try/catch. The result wiil be Left if there's an exception.
     try {
         $eitherResult = call_user_func_array($f, $args);
     } catch (\Exception $e) {
         $eitherResult = Either::left($e->getMessage());
     }
     return $eitherResult;
 }
Beispiel #9
0
 public function map(callable $f)
 {
     // Since we don't know if $f will throw an exception, we wrap the call
     // in a try/catch. The result wiil be Left if there's an exception.
     try {
         $eitherResult = Either::fromValue($f($this->val));
     } catch (\Exception $e) {
         $eitherResult = self::fail($e->getMessage());
     }
     return $eitherResult;
 }
 public function testApplicativeForLeft()
 {
     // The function
     $eitherFn = Either::left('There is no function!');
     // The args
     $eitherStr = Either::fromValue('Hello world!');
     $eitherStart = Either::fromValue(6);
     $eitherLength = Either::fromValue(5);
     // Apply the function applicatively
     $eitherResult = $eitherFn($eitherStr, $eitherStart, $eitherLength);
     $expectedResult = $eitherFn;
     $this->assertEquals($expectedResult, $eitherResult);
 }
 public function testAssociativity()
 {
     // we'll combine four values in two different ways and make sure the
     // results are the same.
     $right1 = Either::fromValue(1);
     $right2 = Either::fromValue(2);
     $right3 = Either::fromValue(3);
     $right4 = Either::fromValue(4);
     // First, combine the first two, then the last two and finally combine
     // the two results.
     $firstTwo = $right1->append($right2);
     $secondTwo = $right3->append($right4);
     $result1 = $firstTwo->append($secondTwo);
     // Next, combine the first two with the third and then combine that
     // result with the fourth.
     $firstThree = $firstTwo->append($right3);
     $result2 = $firstThree->append($right4);
     $this->assertEquals($result1, $result2);
 }
 public function __construct()
 {
     $this->maybet = new MaybeT(Either::left(''));
 }
 public function visitNothing($nothing)
 {
     return Either::left($this->leftVal);
 }