summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Schneider2022-10-03 16:02:35 +0200
committerChristian Schneider2022-10-03 16:02:35 +0200
commit53e71626424285dbed893ee615c98fa378c30b19 (patch)
tree331d8df8ba6badbdb3af10876d3c7b779034469d
parent6d649237a4dae8cc6a1537902c1cfd8a3870243d (diff)
downloaditools-53e71626424285dbed893ee615c98fa378c30b19.tar.gz
itools-53e71626424285dbed893ee615c98fa378c30b19.tar.bz2
itools-53e71626424285dbed893ee615c98fa378c30b19.zip
Fix it::map with generators which can only be used once
-rw-r--r--it.class2
-rwxr-xr-xtest/it.t3
2 files changed, 4 insertions, 1 deletions
diff --git a/it.class b/it.class
index 2079e84..c7d43cb 100644
--- a/it.class
+++ b/it.class
@@ -1111,7 +1111,7 @@ static function map($code, $array, $p = null)
{
$func = self::createfunc($code);
$result = is_array($array) ? $array : iterator_to_array($array);
- foreach (isset($p['keys']) ? it::filter_keys($array, $p['keys']) : $array as $k => $v)
+ foreach (isset($p['keys']) ? it::filter_keys($result, $p['keys']) : $result as $k => $v)
$result[$k] = $func($k, $v);
return (array)$result;
diff --git a/test/it.t b/test/it.t
index 0aee958..7733c00 100755
--- a/test/it.t
+++ b/test/it.t
@@ -490,6 +490,9 @@ is(it::map('it::ucfirst', ["aaa"]), ["Aaa"]);
$dom = new DOMDocument;
$dom->loadXML('<foo>42</foo>');
is(it::map('$v->C14N()', $dom->childNodes), [ '<foo>42</foo>' ], "Traversable: Needs copy, not modifiable in-place");
+# Generator function
+function _generator() { foreach (range(1,2) as $v) yield $v; }
+is(it::map('2*$v', _generator()), [2, 4], "Generator");
# Special values which are not callable
is(it::map('null', ["aaa", "aa"]), [null, null]);
is(it::map('1', ["aaa", "aa"]), [1, 1]);