예제 #1
0
파일: tests.php 프로젝트: Warbo/php-prelude
}, 'skip2' => function () {
    return array_map(skip(1, 'id'), ['a', 'b', 'c'], ['x', 'y', 'z']) !== ['x', 'y', 'z'];
}, 'curry' => function () {
    return id('plus', 5, 3) !== 8;
}, 'curry_n' => function () {
    return curry_([], 3, 'array_merge', [1], [2], [3]) !== [1, 2, 3];
}, 'nary' => function () {
    $n = mt_rand(2, 50);
    return uncurry(nary(function () {
        return sum(func_get_args());
    }, $n), range(1, $n)) !== sum(range(1, $n));
}, 'arity1' => function () {
    return arity(function ($a, $b) {
    }) !== 2;
}, 'arity2' => function ($n) {
    return arity(nary(function () {
    }, $n % 100)) !== $n % 100;
}, 'arity3' => function () {
    return arity('plus') !== 2;
}, 'arity4' => function () {
    return arity(plus(2)) !== 1;
}, 'arity5' => function () {
    return arity(flip('plus')) !== 2;
}, 'key_map' => function () {
    return key_map('plus', [1 => 2, 4 => 8, 16 => 32]) !== [1 => 3, 4 => 12, 16 => 48];
}, 'flip1' => function () {
    return flip(array_(2), 1, 2) !== [2, 1];
}, 'flip2' => function () {
    return flip('map', [-1, -2], plus(5)) !== [4, 3];
}, 'compose1' => function ($x, $y, $z) {
    return call(compose(plus($x), mult($y)), $z) !== $x + $y * $z;
}, 'compose2' => function ($x, $y, $z) {
예제 #2
0
defun('trampoline', function ($f) {
    for ($stop = false; !$stop; list($stop, $f) = $f(null)) {
    }
    return $f;
});
defun('y', function ($f) {
    $cf = curry($f);
    return curry(function ($x) use($cf) {
        return $cf(y($cf), $x);
    });
});
defun('stream_take', function ($n, $s) {
    return trampoline(y(function ($f, $x, $n, $s, $_) {
        if (!$n) {
            return [true, $x];
        }
        list($h, $t) = $s(null);
        return [false, $f(snoc($h, $x), $n - 1, $t)];
    }, [], $n, $s));
});
defun('stream_drop', function ($n, $s) {
    return trampoline(y(function ($f, $n, $s, $_) {
        if (!$n) {
            return [true, $s];
        }
        list($h, $t) = $s(null);
        return [false, $f($n - 1, $t)];
    }));
});
defun('dump', nary('var_dump', 1));