Accumulator factory: Difference between revisions
Content added Content deleted
m (→{{header|J}}: eliminate an unnecessary redirect) |
(Add Lang example) |
||
Line 1,879: | Line 1,879: | ||
8.3 |
8.3 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Lang}}== |
|||
Lang does not support closures. The use of combinator functions and pointers allows a function to store state. |
|||
<syntaxhighlight lang="lang"> |
|||
fp.accumulator = ($sum) -> { |
|||
$sumPtr = $[sum] |
|||
fp.f = ($sumPtr, $n) -> { |
|||
$*sumPtr += $n |
|||
return $*sumPtr |
|||
} |
|||
return fn.argCnt1(fn.combA2(fp.f, $sumPtr)) |
|||
} |
|||
$x = fp.accumulator(1) |
|||
fn.println($x(5)) |
|||
fp.accumulator(3) |
|||
fn.println($x(2.3)) |
|||
fn.println() |
|||
$y = fp.accumulator(1.) |
|||
fn.println($y(5)) |
|||
fn.println($y(2.3)) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
6 |
|||
8.3 |
|||
6.0 |
|||
8.3 |
|||
</pre> |
|||
=={{header|LFE}}== |
=={{header|LFE}}== |