Accumulator factory: Difference between revisions

added RPL
(Add Ursalang example)
(added RPL)
(3 intermediate revisions by 3 users not shown)
Line 1,022:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">function(acc)
= (n => acc.append:(n));
 
accumulator(n)
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,920 ⟶ 3,962:
echo <={y -3000}</syntaxhighlight>
 
== {{header|Ursalang}} ==
Ursalang has only a single number type.
Line 3,978 ⟶ 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