of() public static method

public static of ( $value, Widmogrod\FantasyLand\Monoid $side = null )
$side Widmogrod\FantasyLand\Monoid
示例#1
0
 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());
    }
示例#3
0
/**
 * log :: Writer s w => w -> m a
 *
 * Add a log to the writer without modifying the value
 *
 * @param FantasyLand\Monoid $log
 *
 * @return callable
 */
function log(FantasyLand\Monoid $log)
{
    return function ($value) use($log) {
        return M\Writer::of($value, $log);
    };
}