示例#1
0
<?php

namespace Laiz\Func;

use Laiz\Func;
use Laiz\Func\Any;
use Laiz\Func\Unit;
Loader::load();
function c(callable $f)
{
    return new Func\Curry($f);
}
function f(callable $f, ...$args)
{
    if (!$f instanceof Func\Func) {
        $f = new Func\Func($f);
    }
    if ($args) {
        $f = $f(...$args);
    }
    return $f;
}
// Func
function compose(...$args)
{
    return f(function (callable $g, callable $f, $a) {
        return $g($f($a));
    }, ...$args);
}
function id(...$args)
{
示例#2
0
<?php

namespace Laiz\Test\Func;

use Laiz\Func;
\Laiz\Func\Loader::load();
class AnyTest extends \PHPUnit_Framework_TestCase
{
    public function testWriterMonad()
    {
        $ret = runWriter(ret(1)->bind(function ($_) {
            return ret(2);
        })->bind(function ($a) {
            return tell("a")->bind(function ($_) use($a) {
                return ret($a + 5);
            });
        }));
        $this->assertEquals([7, "a"], $ret);
    }
}