September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,19 +1,16 @@
|
|||
import com.google.common.base.Function;
|
||||
public class Accumulator {
|
||||
private double sum;
|
||||
public Accumulator(double sum0) {
|
||||
sum = sum0;
|
||||
}
|
||||
public double call(double n) {
|
||||
return sum += n;
|
||||
}
|
||||
|
||||
public class AccumulatorFactory {
|
||||
private static Function<Double, Double> accumulator(final Double elem) {
|
||||
return new Function<Double, Double>() {
|
||||
Double sum = elem;
|
||||
@Override public Double apply(Double val) {
|
||||
return sum += val;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Function<Double, Double> x = accumulator(1d);
|
||||
x.apply(5d);
|
||||
System.out.println(accumulator(3d));
|
||||
System.out.println(x.apply(2.3));
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,24 @@
|
|||
import java.util.function.DoubleUnaryOperator;
|
||||
public class AccumulatorFactory {
|
||||
|
||||
public interface AccumulatorFactory {
|
||||
public static DoubleUnaryOperator accumulator(double element) {
|
||||
double[] sum = new double[] { element };
|
||||
return value -> sum[0] += value;
|
||||
}
|
||||
public interface Accumulator {
|
||||
double add(double x);
|
||||
}
|
||||
|
||||
public static void main(String... arguments) {
|
||||
DoubleUnaryOperator x = accumulator(1d);
|
||||
x.applyAsDouble(5d);
|
||||
System.out.println(accumulator(3d));
|
||||
System.out.println(x.applyAsDouble(2.3));
|
||||
}
|
||||
private static Accumulator accumulator(final double initial) {
|
||||
return new Accumulator() {
|
||||
private double sum = initial;
|
||||
|
||||
@Override
|
||||
public double add(double x) {
|
||||
return sum += x;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Accumulator x = accumulator(1.0);
|
||||
x.add(5.0);
|
||||
System.out.println(accumulator(3.0));
|
||||
System.out.println(x.add(2.3));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,15 @@
|
|||
public class Accumulator {
|
||||
private Long sumA; // non-null if we're working in the integer domain
|
||||
private double sumB;
|
||||
public Accumulator(Number sum0) {
|
||||
if (sum0 instanceof Double) {
|
||||
sumB = sum0.doubleValue();
|
||||
} else {
|
||||
sumA = sum0.longValue();
|
||||
}
|
||||
}
|
||||
public Number call(Number n) {
|
||||
if (sumA != null) {
|
||||
if (n instanceof Double) {
|
||||
sumB = n.doubleValue() + sumA;
|
||||
sumA = null;
|
||||
return sumB;
|
||||
}
|
||||
return sumA += n.longValue();
|
||||
}
|
||||
return sumB += n.doubleValue();
|
||||
}
|
||||
import java.util.function.DoubleUnaryOperator;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Accumulator x = new Accumulator(1);
|
||||
x.call(5);
|
||||
Accumulator y = new Accumulator(3);
|
||||
System.out.println(y+" has value "+y.call(0));
|
||||
System.out.println(x.call(2.3));
|
||||
}
|
||||
public interface AccumulatorFactory {
|
||||
public static DoubleUnaryOperator accumulator(double element) {
|
||||
double[] sum = { element };
|
||||
return value -> sum[0] += value;
|
||||
}
|
||||
|
||||
public static void main(String... arguments) {
|
||||
DoubleUnaryOperator x = accumulator(1.0);
|
||||
x.applyAsDouble(5.0);
|
||||
System.out.println(accumulator(3.0));
|
||||
System.out.println(x.applyAsDouble(2.3));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
30
Task/Accumulator-factory/Java/accumulator-factory-5.java
Normal file
30
Task/Accumulator-factory/Java/accumulator-factory-5.java
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
public class Accumulator {
|
||||
private Long sumA; // non-null if we're working in the integer domain
|
||||
private double sumB;
|
||||
public Accumulator(Number sum0) {
|
||||
if (sum0 instanceof Double) {
|
||||
sumB = sum0.doubleValue();
|
||||
} else {
|
||||
sumA = sum0.longValue();
|
||||
}
|
||||
}
|
||||
public Number call(Number n) {
|
||||
if (sumA != null) {
|
||||
if (n instanceof Double) {
|
||||
sumB = n.doubleValue() + sumA;
|
||||
sumA = null;
|
||||
return sumB;
|
||||
}
|
||||
return sumA += n.longValue();
|
||||
}
|
||||
return sumB += n.doubleValue();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Accumulator x = new Accumulator(1);
|
||||
x.call(5);
|
||||
Accumulator y = new Accumulator(3);
|
||||
System.out.println(y+" has value "+y.call(0));
|
||||
System.out.println(x.call(2.3));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue