summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--it.class3
-rwxr-xr-xtest/it.t3
2 files changed, 6 insertions, 0 deletions
diff --git a/it.class b/it.class
index 0b48229..200829c 100644
--- a/it.class
+++ b/it.class
@@ -1034,6 +1034,9 @@ static function date($format = "", $stamp = null)
# Internal: Convert expression or funcname or function literal to callable
static function createfunc($code)
{
+ if ($code instanceof Closure && (new ReflectionFunction($code))->getNumberOfRequiredParameters() == 1)
+ $code = function($dummy, $v) use ($code) {return $code($v);};
+
if (is_string($code) && it::match('^[\w:]+$', $code) && is_callable($code))
$code .= '($v)';
diff --git a/test/it.t b/test/it.t
index 203a136..49d5854 100755
--- a/test/it.t
+++ b/test/it.t
@@ -448,6 +448,7 @@ is(it::mod(7, 4), 3);
is(it::map('5*$k+$v', [0 => 1, 1 => 2]), [1, 7]);
is(it::map(function($k, $v) {return 5*$k+$v;}, [0 => 1, 1 => 2]), [1, 7]);
+is(it::map(function($v) {return 2*$v;}, [0 => 1, 1 => 2]), [2, 4]);
is(it::map('strlen', ["aaa", "aa"]), [3, 2]);
is(it::map('it::ucfirst', ["aaa"]), ["Aaa"]);
$dom = new DOMDocument;
@@ -465,6 +466,8 @@ is(it::map('2*$v', ['foo' => 1, 'bar' => 2], ['keys' => 'foo']), ['foo' => 2, 'b
# it::filter
is(it::filter('$v > 2', [1, 5 => 2, 2 => 3]), [2 => 3]);
is(it::filter('$k > 2', [1, 5 => 2, 2 => 3]), [5 => 2]);
+is(it::filter(function($v) {return $v > 2;}, [1, 5 => 2, 2 => 3]), [2 => 3]);
+is(it::filter(function($k, $v) {return $k > 2;}, [1, 5 => 2, 2 => 3]), [5 => 2]);
is(it::split("b", "aba"), ["a", "a"]);
is(it::split("b", "aBa"), ["a", "a"]);