Jump to content

Closures/Value capture: Difference between revisions

no edit summary
m (added whitespace and highlighting to the task's preamble.)
No edit summary
Line 1,249:
});
}</lang>
 
=={{header|PowerShell}}==
I'm not sure that I understood the question/task. This task seems to be the same as the 'Accumulator Factory' task.
<lang PowerShell>
function Get-Closure ([double]$Number)
{
{param([double]$Sum) return $script:Number *= $Sum}.GetNewClosure()
}
</lang>
<lang PowerShell>
for ($i = 1; $i -lt 11; $i++)
{
$total = Get-Closure -Number $i
 
[PSCustomObject]@{
Function = $i
Sum = & $total -Sum $i
}
}
</lang>
{{Out}}
<pre>
Function Sum
-------- ---
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
</pre>
<lang PowerShell>
$numbers = 1..20 | Get-Random -Count 10
 
foreach ($number in $numbers)
{
$total = Get-Closure -Number $number
 
[PSCustomObject]@{
Function = $number
Sum = & $total -Sum $number
}
}
</lang>
{{Out}}
<pre>
Function Sum
-------- ---
4 16
16 256
3 9
17 289
9 81
15 225
7 49
6 36
1 1
20 400
</pre>
 
=={{header|Prolog}}==
308

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.