add java version

This commit is contained in:
overflowerror 2020-07-08 15:48:29 +02:00
parent 9cdc05b70e
commit 0363c0a329
9 changed files with 226 additions and 0 deletions

View file

@ -0,0 +1,33 @@
package business;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public abstract class AbstractFibonacciSequence<T> extends RecursiveSeqeunce<T> {
private List<T> startValues = new ArrayList<>(2);
protected abstract T add(T v1, T v2);
public AbstractFibonacciSequence(T value1, T value2) {
startValues.add(value1);
startValues.add(value2);
}
@Override
protected List<T> getInitialState() {
return new ArrayList<>(startValues);
}
@Override
protected T getNext(List<T> last) {
return this.add(last.get(0), last.get(1));
}
public static <T> Consumer<T> prettyPrintConsumer(PrintStream stream, String format) {
return RecursiveSeqeunce.accumulatedConsumer(3, l -> {
stream.printf(format, l.get(0), l.get(1), l.get(2));
});
}
}

27
java/business/Couple.java Normal file
View file

@ -0,0 +1,27 @@
package business;
public class Couple<T1, T2> {
private T1 v1;
private T2 v2;
public Couple(T1 v1, T2 v2) {
this.v1 = v1;
this.v2 = v2;
}
public T1 getV1() {
return v1;
}
public void setV1(T1 v1) {
this.v1 = v1;
}
public T2 getV2() {
return v2;
}
public void setV2(T2 v2) {
this.v2 = v2;
}
}

View file

@ -0,0 +1,18 @@
package business;
public class FibonacciBusinessApplication {
public static void main(String[] args) {
System.out.printf("Result: %d\n",
new IntegerFibonacciSequence(1, 1)
.stream()
.limit(5)
.peek(
AbstractFibonacciSequence.prettyPrintConsumer(
System.out,
"%d + %d = %d\n"
)
).mapToInt(Integer::intValue)
.max()
.getAsInt());
}
}

View file

@ -0,0 +1,12 @@
package business;
public class IntegerFibonacciSequence extends AbstractFibonacciSequence<Integer> {
@Override
protected Integer add(Integer v1, Integer v2) {
return v1 + v2;
}
public IntegerFibonacciSequence(Integer value1, Integer value2) {
super(value1, value2);
}
}

View file

@ -0,0 +1,49 @@
package business;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
public abstract class RecursiveSeqeunce<T> implements Sequence<T> {
protected abstract List<T> getInitialState();
protected abstract T getNext(List<T> last);
private T supplierGetter(Couple<Integer, List<T>> _state) {
List<T> state = _state.getV2();
int size = state.size();
int index = _state.getV1();
_state.setV1(index + 1);
if (index < size) {
return state.get(index);
}
T value = getNext(state);
state.remove(0);
state.add(value);
return value;
}
@Override
public Stream<T> stream() {
return Stream.generate(StatefulSupplier.get(new Couple<>(0, getInitialState()), this::supplierGetter));
}
public static <T> Consumer<T> accumulatedConsumer(int recursionLength, Consumer<List<T>> consumer) {
return StatefulConsumer.get(new ArrayList<T>(recursionLength), (l, o) -> {
if (l.size() >= recursionLength) {
l.remove(0);
}
l.add(o);
if (l.size() >= recursionLength) {
consumer.accept(l);
}
});
}
}

View file

@ -0,0 +1,7 @@
package business;
import java.util.stream.Stream;
public interface Sequence<T> {
Stream<T> stream();
}

View file

@ -0,0 +1,32 @@
package business;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public interface StatefulConsumer<S, T> extends Consumer<T> {
class StatefulConsumerImpl<S, T> implements StatefulConsumer<S, T> {
private S state;
private BiConsumer<S, T> consumer;
private StatefulConsumerImpl(S state, BiConsumer<S, T> consumer) {
this.state = state;
this.consumer = consumer;
}
@Override
public void accept(S state, T value) {
consumer.accept(state, value);
}
@Override
public void accept(T t) {
accept(state, t);
}
}
void accept(S state, T value);
static <S, T> StatefulConsumer<S, T> get(S state, BiConsumer<S, T> consumer) {
return new StatefulConsumerImpl<>(state, consumer);
}
}

View file

@ -0,0 +1,31 @@
package business;
import java.util.function.Function;
import java.util.function.Supplier;
public interface StatefulSupplier<S, T> extends Supplier<T> {
class StatefulSupplierImpl<S, T> implements StatefulSupplier<S, T> {
private S state;
private Function<S, T> getter;
private StatefulSupplierImpl(S state, Function<S, T> getter) {
this.state = state;
this.getter = getter;
}
@Override
public T get(S s) {
return getter.apply(s);
}
public T get() {
return get(state);
}
}
T get(S s);
static <S, T> StatefulSupplier<S, T> get(S state, Function<S, T> getter) {
return new StatefulSupplierImpl<>(state, getter);
}
}

View file

@ -0,0 +1,17 @@
import java.util.Map;
import java.util.stream.Stream;
public class Fibonacci {
public static void main(String[] args) {
System.out.printf("Result: %d\n",
Stream.iterate(
Map.entry(1, 1), e -> Map.entry(e.getValue(), e.getKey() + e.getValue())
).limit(args.length == 1 ? Integer.valueOf(args[0]) : 3
).peek(e -> System.out.printf("%d + %d = ", e.getKey(), e.getValue())
).mapToInt(e -> e.getKey() + e.getValue()
).peek(System.out::println
).max(
).getAsInt()
);
}
}