Apply a callback to an array: Difference between revisions

Content added Content deleted
(Undo revision 91391 by 109.125.75.243 (Talk))
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|PureBasic}}==
=={{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 PureBasic>Procedure Cube(Array param.i(1))
<lang powershell>1..5 | ForEach-Object { $_ * $_ }</lang>
Protected n.i
To recreate a ''map'' function, found in other languages the same method applies:
For n = 0 To ArraySize(param())
<lang powershell>function map ([array] $a, [scriptblock] $s) {
Debug Str(param(n)) + "^3 = " + Str(param(n) * param(n) * param(n))
$a | ForEach-Object $s
Next
}</lang>
EndProcedure

Dim AnArray.i(4)

For n = 0 To ArraySize(AnArray())
AnArray(n) = Random(99)
Next

Cube(AnArray()) </lang>


=={{header|Python}}==
=={{header|Python}}==