Example #1
0
 public function testAp()
 {
     $a = Writer::of(5, new Monoid([]));
     $this->assertEquals(Writer::of(function ($x) {
         return $x + 2;
     }, new Monoid([]))->ap($a)->run()[0], 7, 'Applies a parameter.');
 }
Example #2
0
 public function testTell()
 {
     $writer = Writer::of(2, Monoid::empty());
     list($xs, $log) = $writer->chain(function ($x) use($writer) {
         $log = new Monoid(['Hello, world!']);
         return $writer->tell($log)->map(function ($_) use($x) {
             return $x * 2;
         });
     })->run();
     $this->assertEquals($xs, 4, 'Maps correctly.');
     $this->assertEquals($log->value, ['Hello, world!'], 'Logs correctly.');
 }
Example #3
0
 public function testChain()
 {
     $halve = function ($number) {
         $log = new _(['Halving the number']);
         return Writer::tell($log)->map(function () use($number) {
             return $number / 2;
         });
     };
     list($xs, $log) = $halve(16)->chain($halve)->run();
     $this->assertEquals($xs, 4, 'Chains the value.');
     $this->assertEquals($log->value, ['Halving the number', 'Halving the number'], 'Chains the log.');
 }
Example #4
0
 public function testApplicativeConstructor()
 {
     $this->assertEquals(Writer::of(2, Monoid::empty())->run()[0], 2, 'Constructs an applicative.');
 }