Apply a callback to an array: Difference between revisions

added PowerShell
(added PowerShell)
Line 911:
 
If one wants to create a new array consisting of transformed values then procedure mapdata may be more convenient.
 
=={{header|PowerShell}}==
This can be done in PowerShell with the <code>ForEach-Object</code> cmdlet which applies a scriptblock to each element of an array:
<lang powershell>1..5 | ForEach-Object { $_ * $_ }</lang>
To recreate a ''map'' function, found in other languages the same method applies:
<lang powershell>function map ([array] $a, [scriptblock] $s) {
$a | ForEach-Object $s
}</lang>
 
=={{header|Python}}==
Anonymous user