Accumulator factory: Difference between revisions

added RPL
(→‎{{header|Bracmat}}: Added a solution that handles floating point values,)
(added RPL)
(7 intermediate revisions by 5 users not shown)
Line 1,022:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">function(acc)
= (n => acc.append:(n));
 
accumulator(n)
Line 1,086:
|In the following code we are telling EMal that int and real implement
|the Number virtual interface, so that it can only
|accept null (because it's is an interface), int, and real values.
|^
type Number allows int, real
Line 1,115:
type Main
^|we have developed two solutions,
|it's is time to create a list holding both data types.
|We iterate over the solutions in order to test them.
|^
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}}==
Line 3,282 ⟶ 3,320:
6
8.30
</pre>
 
=={{header|RPL}}==
 
This implementation complies with all the rules except maybe the last one ("Doesn't store the accumulated value or the returned functions in a way that could cause them to be inadvertently modified by other code"). The accumulated value is actually stored in a global variable, but as its name is generated with the system time, the likelihood of another code guessing it is very low - unless that code deliberately intends to do so.
{{works with|HP|48}}
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
"M" TIME →STR + SWAP
OVER OBJ→ STO
"≪ '" SWAP + "' STO+ SWAP DROP RCL ≫" + OBJ→
≫ ‘<span style="color:blue">FOO</span>’ STO
|
<span style="color:blue">FOO</span> ''( n → ≪ accumulator ≫ ) ''
create a global variable with a timestamp name
initialize variable with n
create lambda function
.
|}
Let's check it works:
{| class="wikitable" ≪
! Command line
! Test example
|-
|
1 <span style="color:blue">FOO</span> 'X' STO
5 X DROP
3 <span style="color:blue">FOO</span> DROP
2.3 X
|
x = foo(1); <span style="color:grey">// X contains ≪ 'M17.3741285888' STO+ LASTARG SWAP DROP RCL ≫</span>
x(5);
foo(3);
print x(2.3);
|}
{{out}}
<pre>
1: 8.3
</pre>
 
Line 3,881 ⟶ 3,961:
echo <={x 2.3}
echo <={y -3000}</syntaxhighlight>
 
== {{header|Ursalang}} ==
Ursalang has only a single number type.
<syntaxhighlight lang="ursalang">let fac = fn(n) {
fn(i) {
n := n + i
}
}
 
let x = fac(1)
x(5)
fac(3)
print(x(2.3))</syntaxhighlight>
 
=={{header|VBScript}}==
Line 3,926 ⟶ 4,020:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var accumulator = Fn.new { |acc| Fn.new { |n| acc = acc + n } }
 
var x = accumulator.call(1)
1,150

edits