Accumulator factory: Difference between revisions
Content added Content deleted
imported>Arakov |
(added RPL) |
||
Line 3,320: | Line 3,320: | ||
6 |
6 |
||
8.30 |
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> |
</pre> |
||