Accumulator factory: Difference between revisions

added RPL
m (→‎{{header|J}}: eliminate an unnecessary redirect)
(added RPL)
(9 intermediate revisions by 7 users not shown)
Line 496:
 
=={{header|Bracmat}}==
NoticeUntil that2023 Bracmat hashad no facility for handling floating point numbers,. This solution handles only rational numbers.
<syntaxhighlight lang="bracmat">( ( accumulator
=
Line 514:
Output:
<pre>83/10</pre>
 
The following solution uses UFP (UnIfancyfied Floating Point) objects to handle the terms in case not both are rational numbers.
<syntaxhighlight lang="bracmat">( ( accumulator
=
.
' ( add sum object addFunction
. ( addFunction
= A B
. !arg:(?A.?B)
& ( !A:#
& !B:#
& "If both values are recognized as integer or fractional values, just use '+'."
& !A+!B
| "Otherwise, create an object for adding two C doubles and let that run."
& ( new
$ (UFP,'(.$($A)+$($B)))
. go
)
$
)
)
& ( object
= add
= addFunction$($arg.!arg)
)
& !(object.add):?sum
& 'addFunction$($($sum).!arg)
: (=?(object.add))
& !sum
)
)
& accumulator$1:(=?x)
& x$5
& accumulator$1:(=?y)
& y$"5.0"
& out$(x$23/10)
& out$(y$"2.3")
)</syntaxhighlight>
 
Output
<pre>83/10
8.3000000000000007E+00</pre>
 
=={{header|Brat}}==
Line 980 ⟶ 1,022:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">function(acc)
= (n => acc.append:(n));
 
accumulator(n)
Line 1,044 ⟶ 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,073 ⟶ 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 1,879 ⟶ 1,921:
8.3
</syntaxhighlight>
 
=={{header|Lang}}==
Lang does not support closures. The use of combinator functions and pointers allows a function to store state.
<syntaxhighlight lang="lang">
fp.accumulator = ($sum) -> {
$sumPtr = $[sum]
fp.f = ($sumPtr, $n) -> {
$*sumPtr += $n
return $*sumPtr
}
return fn.argCnt1(fn.combA2(fp.f, $sumPtr))
}
 
$x = fp.accumulator(1)
fn.println($x(5))
fp.accumulator(3)
fn.println($x(2.3))
 
fn.println()
 
$y = fp.accumulator(1.)
fn.println($y(5))
fn.println($y(2.3))
</syntaxhighlight>
 
{{out}}
<pre>
6
8.3
 
6.0
8.3
</pre>
 
=={{header|LFE}}==
Line 2,193 ⟶ 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,204 ⟶ 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,803 ⟶ 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,848 ⟶ 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