Esempio n. 1
0
/*
 * A sane, functional wrapper for PHP's mishmash of incompatible primitives,
 * procedures, methods, tokens, etc.
 *
 * Written by Chris Warburton ( ChrisWarbo@GMail.com ) and released into the
 * Public Domain
 */
// Errors are unacceptable
set_error_handler(function () {
    var_dump(array('args' => func_get_args(), 'trace' => debug_backtrace()));
    die;
});
defuns(['skip' => function ($n, $f) {
    return nary(function () use($n, $f) {
        return uncurry($f, array_slice(func_get_args(), $n));
    }, $n + arity($f));
}, 'nary' => function ($f, $n) {
    $args = abs($n) ? '$a' . implode(', $a', range(0, $n - 1)) : '';
    return curry(eval("return function({$args}) use (\$f) {\n                                  return \$f({$args});\n                                };"));
}]);
// Replace operators with proper functions //
defuns(['minus' => function ($x) {
    return -$x;
}, 'bNot' => function ($x) {
    return !$x;
}, 'clone_' => function ($x) {
    return clone $x;
}, 'plus' => function ($x, $y) {
    return $x + $y;
}, 'sub' => function ($x, $y) {
Esempio n. 2
0
}, 'sample1' => function ($n) {
    return sample('id', 4) !== upto(4);
}, 'sample2' => function ($n) {
    return sample('id', $n % 100) !== upto($n % 100);
}, 'skip1' => function ($x) {
    return skip(1, 'id', $x, false);
}, '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];
Esempio n. 3
0
}, 'instanceof spots non-instances' => function () {
    $o = new Exception();
    $result = call('instanceof', $o, 'stdClass');
    return $result ? get_defined_vars() : 0;
}, 'array is callable' => function ($x, $y, $z) {
    $lhs = call('array', $x, $y, $z);
    return $lhs === array($x, $y, $z) ? 0 : get_defined_vars();
}, 'not is callable' => function ($x, $y) {
    $f = op('!');
    $lhs = $f($x > $y);
    $rhs = !($x > $y);
    return $lhs === $rhs ? 0 : get_defined_vars();
}, 'not is curried' => function ($x, $y) {
    $lhs = op('!', $x > $y);
    $rhs = !($x > $y);
    return $lhs === $rhs ? 0 : get_defined_vars();
}, 'can define functions' => function ($x, $y, $z) {
    $name = "func{$x}";
    $f = defun($name, function ($n) use($y) {
        return $y + $n;
    });
    $result = $name($z);
    return $result === $y + $z ? 0 : get_defined_vars();
}, 'uncurry uncurries' => function ($x, $y, $z) {
    $f = uncurry(function ($a, $b, $c) {
        return $a + $b + $c;
    });
    $result = $f(func_get_args());
    return $result === $x + $y + $z ? 0 : get_defined_vars();
}));
$failures ? var_dump(array('Test failures' => $failures)) : (print "All tests passed\n");