Apply a callback to an array: Difference between revisions
Content added Content deleted
Line 1,010: | Line 1,010: | ||
If one wants to create a new array consisting of transformed values then procedure mapdata may be more convenient. |
If one wants to create a new array consisting of transformed values then procedure mapdata may be more convenient. |
||
=={{header| |
=={{header|PureBasic}}== |
||
<lang PureBasic>Procedure Cube(Array param.i(1)) |
|||
This can be done in PowerShell with the <code>ForEach-Object</code> cmdlet which applies a scriptblock to each element of an array: |
|||
Protected n.i |
|||
<lang powershell>1..5 | ForEach-Object { $_ * $_ }</lang> |
|||
For n = 0 To ArraySize(param()) |
|||
To recreate a ''map'' function, found in other languages the same method applies: |
|||
Debug Str(param(n)) + "^3 = " + Str(param(n) * param(n) * param(n)) |
|||
<lang powershell>function map ([array] $a, [scriptblock] $s) { |
|||
Next |
|||
$a | ForEach-Object $s |
|||
EndProcedure |
|||
}</lang> |
|||
Dim AnArray.i(4) |
|||
For n = 0 To ArraySize(AnArray()) |
|||
AnArray(n) = Random(99) |
|||
Next |
|||
Cube(AnArray()) </lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |