Accumulator factory: Difference between revisions

added RPL
imported>Chinhouse
No edit summary
(added RPL)
(6 intermediate revisions by 4 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 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,919 ⟶ 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,964 ⟶ 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