Anonymous user
Accumulator factory: Difference between revisions
no edit summary
(→{{header|Bracmat}}: Added a solution that handles floating point values,) |
imported>Chinhouse No edit summary |
||
Line 2,271:
io.format("%d, %d\n", [i(N1), i(N2)], !IO),
io.format("%.0f, %.0f\n", [f(R1), f(R2)], !IO).</syntaxhighlight>
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
Accumulator = function(n)
adder = {"sum": n}
adder.plus = function(n)
self.sum += n
return self.sum
end function
adder.getSum = function(n)
obj = self
_sum = function(n)
return obj.plus(n)
end function
return @_sum
end function
return adder.getSum
end function
acc1 = Accumulator(0)
print acc1(10) // prints 10
print acc1(2) // prints 12
acc2 = Accumulator(1)
print acc2(100) // prints 101
print acc1(0) // prints 12
</syntaxhighlight>
{{out}}
<pre>miniscript.exe accumulator.ms
10
12
101
12</pre>
=={{header|Nemerle}}==
|