Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.claude_martin.foo;
- import java.util.Arrays;
- import java.util.function.BinaryOperator;
- public class SomeClass {
- static class Binary {
- public static Binary ZERO = new Binary(false);
- public static Binary ONE = new Binary(true);
- private final boolean value;
- private Binary(boolean value) {
- this.value = value;
- }
- public static Binary of(boolean b) {
- return b ? ONE : ZERO;
- }
- public Binary add(Binary b) {
- return new Binary(b.get() || value);
- }
- public boolean get() {
- return value;
- }
- @Override
- public String toString() {
- return value ? "1" : "0";
- }
- }
- public static void main(String[] args) {
- Binary[] numbers = new Binary[] { Binary.ZERO, Binary.ZERO, Binary.ONE, Binary.ZERO };
- System.out.println(sum(numbers, Binary.ZERO, Binary::add));
- }
- public static <T> T sum(T[] array, T zero, BinaryOperator<T> accumulator) {
- return Arrays.stream(array).reduce(zero, accumulator);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement