Java Programming Examples
Str PA
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
class Str {
private final Supplier contents;
private final Function, String> log;
private Str(Supplier contents) {
this.contents = contents;
this.log = action -> {
String value = contents.get();
action.accept(“traced Str: ” + value);
return value;
};
}
private Str(
Supplier contents,
Function, String> log) {
this.contents = contents;
this.log = log;
}
static Str of(String contents) {
return new Str(() -> contents);
}
static Str of(Supplier contents) {
return new Str(contents);
}
void run(Consumer action) {
action.accept(this.contents.get());
}
void print() {
this.run(x -> System.out.println(x));
}
Str map(Function mapper) {
return new Str(
() -> mapper.apply(this.contents.get()),
action -> {
String value = mapper.apply(this.log.apply(action));
action.accept(“traced map: ” + value);
return value;
});
}
Str flatMap(Function mapper) {
return new Str(
() -> mapper.apply(this.contents.get()).contents.get(),
action -> {
String value = mapper
.apply(this.log.apply(action)).log.apply(action);
action.accept(“traced flatMap: ” + value);
return value;
});
}
Str join(String str) {
return this.map(x -> x + str);
}
Str join(Str str) {
return this.flatMap(x -> str.map(y -> x + y));
}
void trace() {
this.trace(x -> System.out.println(x));
}
void trace(Consumer action) {
action.accept(this.log.apply(action));
}
}
DnC Class
import java.util.List;
import java.util.function.Function;
import java.util.function.Consumer;
import java.util.function.BinaryOperator;
import java.util.function.BiFunction;
import java.util.function.Supplier;
class DnC {
private final Optional> problem;
private final Predicate pred;
private final Function f;
private final Optional,Supplier>>> f2;
private DnC(Optional> p, Predicate pred, Function f, Optional,Supplier>>> f2) {
this.problem = p;
this.pred = pred;
this.f = f;
this.f2 = f2;
}
public static DnC of (T value, Predicate p, Function f) {
return new DnC(Optional.of(() -> value), p, f, Optional.empty());
}
public static DnC of (T value, Predicate p, Function f, Function> f2) {
Function, Supplier>> nf2 = x -> {
Pair tempPair = f2.apply(x);
Pair, Supplier> result = Pair., Supplier>of(() -> tempPair.first(), () -> tempPair.second());
return result;
};
return new DnC (Optional.of(() -> value), p, f, Optional.of(nf2));
}
public static DnC of (Supplier value, Predicate p, Function f, Function,Supplier>> f2) {
return new DnC (Optional.of(value), p, f, Optional.of(f2));
}
public void peek(Consumer action) {
action.accept(this.problem.map(x -> x.get()).orElseThrow());
}
Optional solve() {
Optional temp = this.problem.flatMap(x -> Optional.of(x.get()).filter(this.pred));
return temp.map(this.f);
}
Optional solve(BinaryOperator bOp) {
Optional tempL = this.problem.map(x -> x.get());
Optional tempR = tempL.filter(y -> !this.pred.test(y));
return tempR.flatMap(s -> this.left(tempR).solve(bOp).map(x -> this.right(tempR).solve(bOp).map(y -> bOp.apply(x,y)))).orElseGet(() -> tempL.map(this.f));
}
DnC left() {
Optional temp = this.problem.map(x -> x.get());
return left(temp);
}
private DnC left(Optional supp) {
return supp.filter(x -> !this.pred.test(x))
.flatMap(prob -> this.f2.map(func -> {
return new DnC(Optional.of(func.apply(prob).first()), this.pred, this.f, this.f2);
})).orElse(this);
}
DnC right() {
Optional temp = this.problem.map(x -> x.get());
return right(temp);
}
private DnC right(Optional supp) {
return supp.filter(x -> !this.pred.test(x))
.flatMap(prob -> this.f2.map(func -> {
return new DnC(Optional.of(func.apply(prob).second()), this.pred, this.f, this.f2);
})).orElse(this);
}
Log Class
import java.util.Optional;
import java.util.function.Function;
class Log {
private final T value;
private final String operands;
private Log(T value, String log) {
this.value = value;
this.operands = log;
}
static Log of(T value) {
return Log.of(value, “”);
}
static Log of(T value, String log) {
return Optional.ofNullable(value)
.filter(x -> x instanceof Log> == false)
.flatMap(x -> Optional.ofNullable(log))
.map(x -> new Log(value, x))
.orElseThrow(() -> new IllegalArgumentException(
“Invalid arguments”));
}
public Log map(Function super T, ? extends U> func) {
U newValue = func.apply(this.value);
return Log.of(newValue, this.operands);
}
public Log flatMap(Function super T, ? extends Log extends U>> func) {
Log extends U> newLog = func.apply(this.value);
U newValue = newLog.value;
String newOperands = “”;
if (this.operands.equals(“”)) {
newOperands = newOperands + newLog.operands;
return Log.of(newValue, newOperands);
}
newOperands = this.operands + “\n” + newLog.operands;
return Log.of(newValue, newOperands);
}
public boolean equals(Object obj) {
if (obj instanceof Log> otherLog) {
if (this.value.equals(otherLog.getValue()) &&
this.operands.equals(otherLog.getOperands())) {
return true;
}
}
return false;
}
public String toString() {
if (this.operands.equals(“”)) {
return “Log[” + this.value + “]” +
this.operands;
}
return “Log[” + this.value + “]” + “\n” +
this.operands;
}
private T getValue() {
return this.value;
}
private String getOperands() {
return this.operands;
}
}
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.List;
import java.util.stream.Collectors;
static IntStream twinPrimes(int n) {IntStream stream = IntStream.rangeClosed(2,n)
.filter(x -> (isPrime(x) && isPrime(x + 2)) ||
isPrime(x) && isPrime(x - 2));
return stream;
}
static boolean isPrime(int n) {return n > 1 && IntStream.rangeClosed(2,n - 1)
.allMatch(x -> n % x != 0);
}
static String reverse(String str) { return Stream.of(str.split("")) .reduce((x,y) -> (y + x)).orElse("");}
static long countRepeats(List list) {return IntStream.rangeClosed(0, list.size() - 2)
.mapToObj(x -> (list.get(x) == list.get(x + 1)) &&
(x + 2 > list.size() - 1 ||
list.get(x + 1) != list.get(x + 2)))
.filter(x -> x)
.count();
}
static UnaryOperator> generateRule() {return list -> IntStream.rangeClosed(0, list.size() - 1)
.map(x -> list.get(x) == 1 ? 0 :
((x > 0 && x
list.get(x - 1) == 1 && list.get(x + 1) == 0)
||
(x > 0 && x
list.get(x - 1) == 0 && list.get(x + 1) == 1)
||
(x == 0 && list.get(x + 1) == 1)
||
(x == list.size() - 1 && list.get(x - 1) == 1)
) ? 1 : 0)
.boxed()
.collect(Collectors.toList());
}
static Stream gameOfLife(List list,
UnaryOperator> rule, int n) {return Stream.iterate(list, prev -> rule.apply(prev))
.limit(n)
.map(x -> x.stream()
.map(y -> y == 0 ? "." : "x")
.collect(Collectors.joining("")));}
}
import java.util.Optional;
class Fraction extends AbstractNum { private Fraction(Frac fraction) {super(fraction);
}
private Fraction(Optional opti) {super(opti);
}
static Fraction of(int n, int d) {Num nom = Num.of(n);
Num denom = Num.of(d);
if (denom.equals(Num.zero())) {denom = Num.zero().sub(Num.zero().succ());
}
return new Fraction(Optional.of(
Frac.of(nom, denom))
.filter(x -> x.first().isValid())
.filter(x -> x.second().isValid()));
}
static Fraction of(Optional opti) {return new Fraction(
opti.filter(x -> x.first().isValid())
.filter(x -> x.second().isValid())
.filter(x -> x.second().sub(Num.one()).isValid()));
}
public Fraction add(Fraction otherFraction) {return Fraction.of(this.opt
.flatMap(x -> otherFraction.opt.map(y ->
Frac.of(x.first().mul(y.second()).add(
x.second().mul(y.first())),
x.second().mul(y.second()))
)));
}
public Fraction sub(Fraction otherFraction) {return Fraction.of(
this.opt.flatMap(x -> otherFraction.opt.map(y ->
Frac.of(x.first().mul(y.second()).sub(
x.second().mul(y.first())),
x.second().mul(y.second()))
)));
}
public Fraction mul(Fraction otherFraction) {return Fraction.of(
this.opt.flatMap(x -> otherFraction.opt.map(y ->
Frac.of(x.first().mul(y.first()),
x.second().mul(y.second()))
)));
}
}
import java.util.stream.Stream;
import java.util.Optional;
class Num extends AbstractNum { private Num(int i) {super(i);
}
private Num(AbstractNum abs) {super(abs.opt);
}
private Num(Optional opti) {super(opti);
}
static Num of(int i) {return new Num(new Num(i).opt.filter(valid));
}
static Num of(Optional opti) {return new Num(opti.filter(valid));
}
static Num zero() {return new Num(AbstractNum.zero());
}
public Num succ() {return new Num(this.opt.map(s));
}
static Num one() {return Num.zero().succ();
}
public Num add(Num natural) {Num counter = new Num(natural.opt.map(n)).succ();
Num toReturn = this;
if (this.isValid()) { if (natural.isValid()) { while (Num.of(counter.opt.map(n).filter(valid)).isValid()) {toReturn = toReturn.succ();
counter = counter.succ();
}
return toReturn;
}
return natural;
}
return this;
}
public Num mul(Num natural) {Num counter = new Num(natural.opt.map(n)).succ().succ();
Num toReturn = this;
Num toAdd = this;
if (this.equals(Num.zero())) { if (this.isValid()) { if (natural.isValid()) {return Num.zero();
}
}
}
if (natural.equals(Num.zero())) { if (this.isValid()) { if (natural.isValid()) {return Num.zero();
}
}
}
if (this.isValid()) { if (natural.isValid()) { while (new Num(counter.opt.map(n).filter(valid)).isValid()) {toReturn = toReturn.add(toAdd);
counter = counter.succ();
}
return toReturn;
}
return natural;
}
return this;
}
public Num sub(Num natural) {Num toReturn = this;
if (this.isValid()) { if (natural.isValid()) {toReturn = new Num(natural.opt.map(n))
.add(toReturn);
toReturn = Num.of(toReturn.opt);
return toReturn;
}
return natural;
}
return this;
}
}
