Skip to content

Instantly share code, notes, and snippets.

@jbgi
Last active Jan 25, 2016
Embed
What would you like to do?
Lists.java genrated by Derive4J for the List data type (see exemples)
package org.derive4j.exemple;
import java.lang.Object;
import java.lang.Override;
import java.lang.SuppressWarnings;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
public final class Lists {
@SuppressWarnings("rawtypes")
private static final List nil = new Nil();
@SuppressWarnings("rawtypes")
private static final TotalMatchBuilderNil<Object> totalMatchBuilderNil = new TotalMatchBuilderNil<Object>();
private Lists() {
}
@SuppressWarnings("unchecked")
public static <A> List<A> nil() {
return nil;
}
public static <A> List<A> cons(A head, List<A> tail) {
return new Cons<>(head, tail);
}
public static <A> List<A> lazy(Supplier<List<A>> list) {
return new Lazy<>(list);
}
public static <A, X> Function<List<A>, X> cata(Supplier<X> nil, BiFunction<A, Supplier<X>, X> cons) {
return new Object() {
Function<List<A>, X> cata = list -> list.list(
nil,
(head, tail) -> cons.apply(head, () -> this.cata.apply(tail)));
}.cata;
}
public static <A> Optional<A> getHead(List<A> list) {
return list.list(() -> Optional.empty(),
(head, tail) -> Optional.of(head));}
public static <A> Optional<List<A>> getTail(List<A> list) {
return list.list(() -> Optional.empty(),
(head, tail) -> Optional.of(tail));}
public static <A> Function<List<A>, List<A>> setHead(A newHead) {
return modHead(__ -> newHead);
}
public static <A> Function<List<A>, List<A>> modHead(Function<A, A> headMod) {
return list -> list.list(() -> nil(),
(head, tail) -> cons(headMod.apply(head), tail));
}
public static <A> Function<List<A>, List<A>> setTail(List<A> newTail) {
return modTail(__ -> newTail);
}
public static <A> Function<List<A>, List<A>> modTail(Function<List<A>, List<A>> tailMod) {
return list -> list.list(() -> nil(),
(head, tail) -> cons(head, tailMod.apply(tail)));
}
@SuppressWarnings("unchecked")
public static <A> TotalMatchBuilderNil<A> cases() {
return (TotalMatchBuilderNil<A>) totalMatchBuilderNil;
}
private static final class Nil<A> extends List<A> {
Nil() {
}
@Override
public <X> X list(Supplier<X> nil, BiFunction<A, List<A>, X> cons) {
return nil.get();
}
}
private static final class Cons<A> extends List<A> {
private final A head;
private final List<A> tail;
Cons(A head, List<A> tail) {
this.head = head;
this.tail = tail;
}
@Override
public <X> X list(Supplier<X> nil, BiFunction<A, List<A>, X> cons) {
return cons.apply(this.head, this.tail);
}
}
private static final class Lazy<A> extends List<A> {
private final Object lock = new Object();
private Supplier<List<A>> expression;
private volatile List<A> evaluation;
Lazy(Supplier<List<A>> list) {
this.expression = list;
}
private List<A> eval() {
List<A> _evaluation = this.evaluation;
if (_evaluation == null) {
synchronized (this.lock) {
_evaluation = this.evaluation;
if (_evaluation == null) {
this.evaluation = _evaluation = expression.get();
this.expression = null;
}
}
}
return _evaluation;
}
@Override
public <X> X list(Supplier<X> nil, BiFunction<A, List<A>, X> cons) {
return this.eval().list(nil, cons);
}
}
public static final class TotalMatchBuilderNil<A> {
private TotalMatchBuilderNil() {
}
public final <X> TotalMatchBuilderCons<A, X> nil(Supplier<X> nil) {
return new TotalMatchBuilderCons<>(nil);
}
public final <X> TotalMatchBuilderCons<A, X> nil(X x) {
return this.nil(() -> x);
}
public final <X> PartialMatchBuilder<A, X> cons(BiFunction<A, List<A>, X> cons) {
return new PartialMatchBuilder<>(null, cons);
}
public final <X> PartialMatchBuilder<A, X> cons(X x) {
return this.cons((head, tail) -> x);
}
}
public static final class TotalMatchBuilderCons<A, X> extends PartialMatchBuilder<A, X> {
private TotalMatchBuilderCons(Supplier<X> nil) {
super(nil, null);
}
public final Function<List<A>, X> cons(BiFunction<A, List<A>, X> cons) {
Supplier<X> nil = super.nil;
return list -> list.list(nil, cons);
}
public final Function<List<A>, X> cons(X x) {
return this.cons((head, tail) -> x);
}
}
public static class PartialMatchBuilderCons<A, X> extends PartialMatchBuilder<A, X> {
private PartialMatchBuilderCons(Supplier<X> nil) {
super(nil, null);
}
public final PartialMatchBuilder<A, X> cons(BiFunction<A, List<A>, X> cons) {
return new PartialMatchBuilder<>(super.nil, cons);
}
public final PartialMatchBuilder<A, X> cons(X x) {
return this.cons((head, tail) -> x);
}
}
public static class PartialMatchBuilder<A, X> {
private final Supplier<X> nil;
private final BiFunction<A, List<A>, X> cons;
private PartialMatchBuilder(Supplier<X> nil, BiFunction<A, List<A>, X> cons) {
this.nil = nil;
this.cons = cons;
}
public final Function<List<A>, X> otherwise(Supplier<X> otherwise) {
Supplier<X> nil = (this.nil != null) ? this.nil : () -> otherwise.get();
BiFunction<A, List<A>, X> cons = (this.cons != null) ? this.cons : (head, tail) -> otherwise.get();
return list -> list.list(nil, cons);
}
public final Function<List<A>, X> otherwise(X x) {
return this.otherwise(() -> x);
}
public final Function<List<A>, Optional<X>> otherwiseEmpty() {
Supplier<Optional<X>> nil = (this.nil != null) ? () -> Optional.of(this.nil.get())
: () -> Optional.empty();
BiFunction<A, List<A>, Optional<X>> cons = (this.cons != null) ? (head, tail) -> Optional.of(this.cons.apply(head, tail))
: (head, tail) -> Optional.empty();
return list -> list.list(nil, cons);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment