diff options
-rw-r--r-- | it.class | 10 | ||||
-rwxr-xr-x | tests/it.t | 5 |
2 files changed, 9 insertions, 6 deletions
@@ -938,16 +938,16 @@ static function date($format = "", $stamp = null) } /** - * Iterate over an array, replacing every element by expression - * @param $expression The expression to apply, may contain $k for keys and $v for values + * Iterate over an array, replacing every element by function value or expression + * @param $code The function or expression to apply. Expression may contain $k for keys and $v for values * @param $array The array to iterate over */ -static function map($expression, $array) +static function map($code, $array) { static $cache = array(); - if (!($func = $cache[$expression])) - $func = $cache[$expression] = create_function('$k,$v', "return $expression;"); + if (!is_callable($func = $code) && !($func = $cache[$code])) + $func = $cache[$code] = create_function('$k,$v', "return $code;"); foreach ($array as $k => $v) $result[$k] = $func($k, $v); @@ -3,7 +3,6 @@ # Tests for it.class - # # tests for it::match() # @@ -415,3 +414,7 @@ is(it::mod(-9, 4), 3); is(it::mod(-8, 4), 0); is(it::mod(0, 4), 0); is(it::mod(7, 4), 3); + +is(it::map('5*$k+$v', array(1, 2)), array(1, 7)); +is(it::map(create_function('$k,$v', 'return 5*$k+$v;'), array(1, 2)), array(1, 7)); +is(it::map(function($k, $v) {return 5*$k+$v;}, array(1, 2)), array(1, 7)); |