Apply a callback to an array: Difference between revisions

→‎{{header|Vorpal}}: Added Vim Script
No edit summary
(→‎{{header|Vorpal}}: Added Vim Script)
Line 2,421:
1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489, 10000000000
</pre>
 
=={{header|Vim Script}}==
<code>map()</code> works with lists and dictionaries. The second argument is an expression string where <code>v:val</code> is replaced by the current value and <code>v:key</code> by the current key (for lists the key is the index). The result of evaluating the string will be the new value. The list/dictionary is modified in place.
<lang vim>echo map([10, 20, 30], 'v:val * v:val')
echo map([10, 20, 30], '"Element " . v:key . " = " . v:val')
echo map({"a": "foo", "b": "Bar", "c": "BaZ"}, 'toupper(v:val)')
echo map({"a": "foo", "b": "Bar", "c": "BaZ"}, 'toupper(v:key)')</lang>
 
{{Out}}
<pre>[100, 400, 900]
['Element 0 = 10', 'Element 1 = 20', 'Element 2 = 30']
{'a': 'FOO', 'b': 'BAR', 'c': 'BAZ'}
{'a': 'A', 'b': 'B', 'c': 'C'}</pre>
 
=={{header|Vorpal}}==