June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,5 +1,5 @@
|
|||
fun accumulator(sum): |n| -> sum += n
|
||||
let f = accumulator(5)
|
||||
print f(5) # 10
|
||||
print f(10) # 20
|
||||
fun accumulator(sum) = |n| => sum += n
|
||||
let f = accumulator(5.)
|
||||
print f(5) # 10.0
|
||||
print f(10) # 20.0
|
||||
print f(2.4) # 22.4
|
||||
|
|
|
|||
|
|
@ -1,21 +1,16 @@
|
|||
import system'dynamic.
|
||||
function =
|
||||
(:acc)((:n)(acc append:n)).
|
||||
|
||||
Function =
|
||||
(:x)(closure append:x).
|
||||
accumulator =
|
||||
(:n)(function(Variable new(n))).
|
||||
|
||||
extension op
|
||||
{
|
||||
accumulatorOf:func
|
||||
= Variable new(self); mixInto(func).
|
||||
}
|
||||
|
||||
program =
|
||||
public program =
|
||||
[
|
||||
var x := 1 accumulatorOf(Function).
|
||||
var x := accumulator(1).
|
||||
|
||||
x eval(5).
|
||||
x(5).
|
||||
|
||||
var y := 3 accumulatorOf(Function).
|
||||
var y := accumulator(3).
|
||||
|
||||
console write(x eval(2.3r)).
|
||||
console write(x(2.3r)).
|
||||
].
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
USE: locals
|
||||
:: accumulator ( n! -- quot ) [ n + dup n! ] ;
|
||||
|
||||
1 accumulator
|
||||
|
|
|
|||
|
|
@ -1,16 +1,29 @@
|
|||
public class Accumulator {
|
||||
private double sum;
|
||||
public Accumulator(double sum0) {
|
||||
sum = sum0;
|
||||
public class Accumulator
|
||||
//implements java.util.function.UnaryOperator<Number> // Java 8
|
||||
{
|
||||
private Number sum;
|
||||
|
||||
public Accumulator(Number sum0) {
|
||||
sum = sum0;
|
||||
}
|
||||
public double call(double n) {
|
||||
return sum += n;
|
||||
|
||||
public Number apply(Number n) {
|
||||
// Acts like sum += n, but chooses long or double.
|
||||
// Converts weird types (like BigInteger) to double.
|
||||
return (longable(sum) && longable(n)) ?
|
||||
(sum = sum.longValue() + n.longValue()) :
|
||||
(sum = sum.doubleValue() + n.doubleValue());
|
||||
}
|
||||
|
||||
private static boolean longable(Number n) {
|
||||
return n instanceof Byte || n instanceof Short ||
|
||||
n instanceof Integer || n instanceof Long;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Accumulator x = new Accumulator(1);
|
||||
x.call(5);
|
||||
System.out.println(new Accumulator(3));
|
||||
System.out.println(x.call(2.3));
|
||||
Accumulator x = new Accumulator(1);
|
||||
x.apply(5);
|
||||
new Accumulator(3);
|
||||
System.out.println(x.apply(2.3));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,27 @@
|
|||
public class Accumulator {
|
||||
private double sum;
|
||||
public Accumulator(double sum0) {
|
||||
sum = sum0;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
public class AccumulatorFactory {
|
||||
|
||||
public static UnaryOperator<Number> accumulator(Number sum0) {
|
||||
// Allows sum[0] = ... inside lambda.
|
||||
Number[] sum = { sum0 };
|
||||
|
||||
// Acts like n -> sum[0] += n, but chooses long or double.
|
||||
// Converts weird types (like BigInteger) to double.
|
||||
return n -> (longable(sum[0]) && longable(n)) ?
|
||||
(sum[0] = sum[0].longValue() + n.longValue()) :
|
||||
(sum[0] = sum[0].doubleValue() + n.doubleValue());
|
||||
}
|
||||
public double call(double n) {
|
||||
return sum += n;
|
||||
|
||||
private static boolean longable(Number n) {
|
||||
return n instanceof Byte || n instanceof Short ||
|
||||
n instanceof Integer || n instanceof Long;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Accumulator x = new Accumulator(1);
|
||||
x.call(5);
|
||||
System.out.println(new Accumulator(3));
|
||||
System.out.println(x.call(2.3));
|
||||
UnaryOperator<Number> x = accumulator(1);
|
||||
x.apply(5);
|
||||
accumulator(3);
|
||||
System.out.println(x.apply(2.3));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
function accumulator(i)
|
||||
f(n) = i += n
|
||||
return f
|
||||
end
|
||||
|
||||
x = accumulator(1)
|
||||
x(5)
|
||||
@show x(5)
|
||||
|
||||
accumulator(3)
|
||||
x(2.3)
|
||||
@show x(2.3)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,2 @@
|
|||
: foo(n)
|
||||
| ch |
|
||||
Channel new dup ->ch send(n) drop
|
||||
#[ ch receive swap + dup ch send drop ] ;
|
||||
: foo( n -- bl )
|
||||
#[ n swap + dup ->n ] ;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
: testfoo
|
||||
| x y z |
|
||||
foo(1) ->x
|
||||
1 foo ->x
|
||||
5 x perform .
|
||||
foo(3) ->y
|
||||
2.3 x perform dup . ", x accumulator value is a " . class .cr
|
||||
10 y perform dup . ", y accumulator value is a " . class .cr
|
||||
foo("aaa") ->z
|
||||
"bbb" z perform dup . ", z accumulator value is a " . class .cr ;
|
||||
3 foo ->y
|
||||
2.3 x perform dup . ", x accumulator value is a" . class .cr
|
||||
10 y perform dup . ", y accumulator value is a" . class .cr
|
||||
"aaa" foo ->z
|
||||
"bbb" z perform dup . ", z accumulator value is a" . class .cr
|
||||
;
|
||||
|
|
|
|||
12
Task/Accumulator-factory/Perl-6/accumulator-factory.pl6
Normal file
12
Task/Accumulator-factory/Perl-6/accumulator-factory.pl6
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
sub accum ($n is copy) { sub { $n += $^x } }
|
||||
|
||||
#Example use:
|
||||
my $a = accum 5;
|
||||
$a(4.5);
|
||||
say $a(.5); # Prints "10".
|
||||
|
||||
# You can also use the "&" sigil to create a function that behaves syntactically
|
||||
# like any other function (i.e. no sigil nor parentheses needed to call it):
|
||||
|
||||
my &b = accum 5;
|
||||
say b 3; # Prints "8".
|
||||
9
Task/Accumulator-factory/Ruby/accumulator-factory-1.rb
Normal file
9
Task/Accumulator-factory/Ruby/accumulator-factory-1.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def accumulator(sum)
|
||||
lambda {|n| sum += n}
|
||||
end
|
||||
|
||||
# mixing Integer and Float
|
||||
x = accumulator(1)
|
||||
x.call(5)
|
||||
accumulator(3)
|
||||
puts x.call(2.3) # prints 8.3
|
||||
15
Task/Accumulator-factory/Ruby/accumulator-factory-2.rb
Normal file
15
Task/Accumulator-factory/Ruby/accumulator-factory-2.rb
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
require 'rational'
|
||||
require 'complex'
|
||||
y = accumulator(Rational(2, 3))
|
||||
puts y[Rational(1, 2)] # 7/6
|
||||
puts y[4] # 31/6
|
||||
puts y[Complex(0, 1)] # 31/6+1i
|
||||
|
||||
t = accumulator(Time.utc(1999, 8, 7, 6, 5))
|
||||
# (Ruby 1.8.6) (Ruby 1.9.2)
|
||||
puts t[4] # Sat Aug 07 06:05:04 UTC 1999 1999-08-07 06:05:04 UTC
|
||||
puts t[-12 * 60 * 60] # Fri Aug 06 18:05:04 UTC 1999 1999-08-06 18:05:04 UTC
|
||||
|
||||
require 'matrix'
|
||||
m = accumulator(Matrix[[1, 2], [3, 4]])
|
||||
puts m[Matrix[[5, 6], [7, 8]]] # Matrix[[6, 8], [10, 12]]
|
||||
9
Task/Accumulator-factory/Ruby/accumulator-factory-3.rb
Normal file
9
Task/Accumulator-factory/Ruby/accumulator-factory-3.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def accumulator(sum)
|
||||
lambda {|n| sum += n}
|
||||
end
|
||||
class << self
|
||||
define_method :x, &accumulator(1)
|
||||
end
|
||||
x(5)
|
||||
accumulator(3)
|
||||
puts x(2.3) # prints 8.3
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
def accumulator(sum)
|
||||
lambda {|n| sum += n}
|
||||
end
|
||||
|
||||
# mixing Integer and Float
|
||||
x = accumulator(1)
|
||||
x.call(5)
|
||||
p accumulator(3) # add some output to show what it returns
|
||||
puts x.call(2.3) # prints 8.3
|
||||
|
||||
# mixing Rational and Complex
|
||||
require 'rational'
|
||||
require 'complex'
|
||||
y = accumulator(Rational(2, 3))
|
||||
y.call(Rational(1, 2))
|
||||
puts y.call(4)
|
||||
puts y.call(Complex(0, 1))
|
||||
puts y.call(Complex.polar(6, 5 * Math::PI / 4))
|
||||
puts x.call(0) # again prints 8.3
|
||||
|
||||
# using other things that have a + method
|
||||
t = accumulator(Time.utc(1999, 8, 7, 6, 5))
|
||||
puts t.call(4) # prints 1999-08-07 06:05:04 UTC
|
||||
|
||||
require 'matrix'
|
||||
m = accumulator(Matrix[[1, 2], [3, 4]])
|
||||
puts m.call(Matrix[[5, 6], [7, 8]])
|
||||
puts t.call(-12 * 60 * 60) # subtracts 12 hours
|
||||
puts y.call(1e200)
|
||||
puts x.call(0) # again prints 8.3
|
||||
4
Task/Accumulator-factory/XLISP/accumulator-factory.xlisp
Normal file
4
Task/Accumulator-factory/XLISP/accumulator-factory.xlisp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(defun accumulator (x)
|
||||
(lambda (n)
|
||||
(setq x (+ n x))
|
||||
x ) )
|
||||
Loading…
Add table
Add a link
Reference in a new issue