Inheritance: implements Widmogrod\FantasyLand\Monad
 public function provideData()
 {
     $addOne = function ($x) {
         return Writer::of($x + 1);
     };
     $addTwo = function ($x) {
         return Writer::of($x + 2);
     };
     return ['writer 0' => ['$f' => $addOne, '$g' => $addTwo, '$x' => 10]];
 }
    public function test_it_should_filter_with_logs()
    {
        $data = [1, 10, 15, 20, 25];
        $filter = function ($i) {
            if ($i % 2 == 1) {
                return W::of(false, S::of("Reject odd number {$i}.\n"));
            } elseif ($i > 15) {
                return W::of(false, S::of("Reject {$i} because it is bigger than 15\n"));
            }
            return W::of(true);
        };
        list($result, $log) = f\filterM($filter, $data)->runWriter();
        $this->assertEquals([10], $result);
        $this->assertEquals('Reject odd number 1.
Reject odd number 15.
Reject 20 because it is bigger than 15
Reject odd number 25.
', $log->extract());
    }
Beispiel #3
0
/**
 * runWriter :: Writer w a -> (a, w)
 *
 * Unwrap a writer monad computation as a function.
 *
 * @param M\Writer $writer
 *
 * @return mixed
 */
function runWriter(M\Writer $writer)
{
    return $writer->runWriter();
}