Esempio n. 1
0
 public function testChain()
 {
     $addTwo = function ($x) {
         return Either::of($x + 2);
     };
     $id = function ($x) {
         return $x;
     };
     $a = Either::of(5);
     $b = new Left(4);
     $this->assertEquals($a->chain($addTwo)->either($id, $id), 7, 'Chains a Right.');
     $this->assertEquals($b->chain($addTwo)->either($id, $id), 4, 'Chains a Left.');
 }
Esempio n. 2
0
 public function testAp()
 {
     $addTwo = Either::of(function ($x) {
         return $x + 2;
     });
     $id = function ($x) {
         return $x;
     };
     $a = Either::of(5);
     $b = new Left(4);
     $this->assertEquals($addTwo->ap($a)->either($id, $id), 7, 'Applies to a Right.');
     $this->assertEquals($addTwo->ap($b)->either($id, $id), 4, 'Applies to a Left.');
 }
Esempio n. 3
0
 public function testTryCatch()
 {
     $f = function ($bad) {
         return function () use($bad) {
             if ($bad) {
                 throw new \Exception();
             }
             return 'No exception';
         };
     };
     $id = function ($x) {
         return $x;
     };
     $this->assertEquals(Either::tryCatch($f(false))->either($id, $id), 'No exception', 'TryCatches a Right.');
     $this->assertInstanceOf('Exception', Either::tryCatch($f(true))->either($id, $id), 'TryCatches a Left.');
 }
Esempio n. 4
0
 /**
  * Apply a wrapped paramater to this wrapped function.
  * @param Either $that The parameter to apply.
  * @return Either The wrapped result.
  */
 public function ap(Either $that) : Either
 {
     return $this->chain(function ($f) use($that) {
         return $that->map($f);
     });
 }