Accumulator factory: Difference between revisions

Content added Content deleted
Line 3,291: Line 3,291:


the above can also be done without a class to hold the block, simply by putting it into another block (i.e. an outer closure for the sum, returning an inner function which updates that sum):
the above can also be done without a class to hold the block, simply by putting it into another block (i.e. an outer closure for the sum, returning an inner function which updates that sum):
{{works with|Smalltalk/X}}
{{works with|any Smalltalk}}
<lang smalltalk>|factory accu|
<lang smalltalk>|factory accu1 accu2|


factory := [:initial |
factory := [:initial |
Line 3,303: Line 3,303:
].
].


accu := factory value:1.
accu1 := factory value:1.
accu value:5.
accu1 value:5.
accu2 := factory value:10.
(accu value:2.3) printCR "-> 8.3 "</lang>
accu2 value:5.
(accu1 value:2.3) printCR. "-> 8.3 (a float)"
(accu2 value:0) printCR. "-> 15 (an integer)"
(accu2 value:22 factorial) printCR. "-> a large integer"</lang>
{{out}}
<pre>8.3
15
1124000727777607680015</pre>


=={{header|Standard ML}}==
=={{header|Standard ML}}==