cyclops.control.Identity Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops-pure Show documentation
Show all versions of cyclops-pure Show documentation
Platform for Functional Reactive Programming with Java 8
The newest version!
package cyclops.control;
import com.oath.cyclops.hkt.Higher;
import com.oath.cyclops.matching.Deconstruct;
import cyclops.data.tuple.*;
import com.oath.cyclops.hkt.DataWitness.identity;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.function.BiFunction;
import java.util.function.Function;
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public final class Identity implements Higher, Iterable, Deconstruct.Deconstruct1, Serializable {
private static final long serialVersionUID = 1L;
public final T value;
public static Identity tailRec(T initial, Function super T, ? extends Identity extends Either>> fn){
Identity extends Either> next[] = new Identity[1];
next[0] = Identity.of(Either.left(initial));
boolean cont = true;
do {
cont = next[0].fold(p -> p.fold(s -> {
next[0] = narrowK(fn.apply(s));
return true;
}, __ -> false));
} while (cont);
return next[0].map(x->x.fold(l->null, r->r));
}
public static Identity of(T value){
return new Identity<>(value);
}
public static Identity fromTuple(Tuple1 t1){
return of(t1._1());
}
public T get(){
return value;
}
public T extract(){
return value;
}
public R fold(Function super T, ? extends R> fn){
return fn.apply(value);
}
public Identity> nest(){
return of(this);
}
public Identity coflatMap(Function super Identity super T>, ? extends R> fn){
return of(fn.apply(this));
}
public Identity map(Function super T,? extends R> fn){
return new Identity<>(fn.apply(value));
}
public Identity flatMap(Function super T,? extends Identity extends R>> fn){
return narrow(fn.apply(value));
}
public Identity zip(Identity extends T2> id,BiFunction super T,? super T2, ? extends R> fn){
return flatMap(a->id.map(b->fn.apply(a,b)));
}
public Tuple1 toTuple(){
return Tuple1.of(value);
}
public Tuple1 toLazyTuple(){
return Tuple1.lazy(()->value);
}
public static Identity narrow(Identity extends T> id){
return (Identity)id;
}
public static Identity narrowK(Higher ds){
return (Identity)ds;
}
@Override
public Iterator iterator() {
return Arrays.asList(value).iterator();
}
@Override
public Tuple1 unapply() {
return Tuple.tuple(value);
}
public static Higher widen(Identity narrow) {
return narrow;
}
@Deprecated
public static class Comprehensions {
public static Identity forEach(Identity id,
Function super T, ? extends Identity> value2,
Function super Tuple2 super T,? super R1>, ? extends Identity> value3,
Function super Tuple3 super T,? super R1,? super R2>, ? extends Identity> value4,
Function super Tuple4 super T, ? super R1, ? super R2,? super R3>, ? extends Identity> value5,
Function super Tuple5, ? extends Identity> value6,
Function super Tuple6, ? extends Identity> value7,
Function super Tuple7, ? extends Identity> value8
) {
return id.flatMap(in -> {
Identity a = value2.apply(in);
return a.flatMap(ina -> {
Identity b = value3.apply(Tuple.tuple(in,ina));
return b.flatMap(inb -> {
Identity c = value4.apply(Tuple.tuple(in,ina,inb));
return c.flatMap(inc->{
Identity d = value5.apply(Tuple.tuple(in,ina,inb,inc));
return d.flatMap(ind->{
Identity e = value6.apply(Tuple.tuple(in,ina,inb,inc,ind));
return e.flatMap(ine->{
Identity f = value7.apply(Tuple.tuple(in,ina,inb,inc,ind,ine));
return f.flatMap(inf->{
Identity g = value8.apply(Tuple.tuple(in,ina,inb,inc,ind,ine,inf));
return g;
});
});
});
});
});
});
});
}
public static Identity forEach(Identity id,
Function super T, ? extends Identity> value2,
Function super Tuple2 super T,? super R1>, ? extends Identity> value3,
Function super Tuple3 super T,? super R1,? super R2>, ? extends Identity> value4,
Function super Tuple4 super T, ? super R1, ? super R2,? super R3>, ? extends Identity> value5,
Function super Tuple5, ? extends Identity> value6,
Function super Tuple6, ? extends Identity> value7
) {
return id.flatMap(in -> {
Identity a = value2.apply(in);
return a.flatMap(ina -> {
Identity b = value3.apply(Tuple.tuple(in,ina));
return b.flatMap(inb -> {
Identity c = value4.apply(Tuple.tuple(in,ina,inb));
return c.flatMap(inc->{
Identity d = value5.apply(Tuple.tuple(in,ina,inb,inc));
return d.flatMap(ind->{
Identity e = value6.apply(Tuple.tuple(in,ina,inb,inc,ind));
return e.flatMap(ine->{
Identity f = value7.apply(Tuple.tuple(in,ina,inb,inc,ind,ine));
return f;
});
});
});
});
});
});
}
public static Identity forEach(Identity id,
Function super T, ? extends Identity> value2,
Function super Tuple2 super T,? super R1>, ? extends Identity> value3,
Function super Tuple3 super T,? super R1,? super R2>, ? extends Identity> value4,
Function super Tuple4 super T, ? super R1, ? super R2,? super R3>, ? extends Identity> value5,
Function super Tuple5, ? extends Identity> value6
) {
return id.flatMap(in -> {
Identity a = value2.apply(in);
return a.flatMap(ina -> {
Identity b = value3.apply(Tuple.tuple(in,ina));
return b.flatMap(inb -> {
Identity c = value4.apply(Tuple.tuple(in,ina,inb));
return c.flatMap(inc->{
Identity d = value5.apply(Tuple.tuple(in,ina,inb,inc));
return d.flatMap(ind->{
Identity e = value6.apply(Tuple.tuple(in,ina,inb,inc,ind));
return e;
});
});
});
});
});
}
public static Identity forEach(Identity id,
Function super T, ? extends Identity> value2,
Function super Tuple2 super T,? super R1>, ? extends Identity> value3,
Function super Tuple3 super T,? super R1,? super R2>, ? extends Identity> value4,
Function super Tuple4 super T, ? super R1, ? super R2,? super R3>, ? extends Identity> value5
) {
return id.flatMap(in -> {
Identity a = value2.apply(in);
return a.flatMap(ina -> {
Identity b = value3.apply(Tuple.tuple(in,ina));
return b.flatMap(inb -> {
Identity c = value4.apply(Tuple.tuple(in,ina,inb));
return c.flatMap(inc->{
Identity d = value5.apply(Tuple.tuple(in,ina,inb,inc));
return d;
});
});
});
});
}
public static Identity forEach(Identity id,
Function super T, ? extends Identity> value2,
Function super Tuple2 super T,? super R1>, ? extends Identity> value3,
Function super Tuple3 super T,? super R1,? super R2>, ? extends Identity> value4
) {
return id.flatMap(in -> {
Identity a = value2.apply(in);
return a.flatMap(ina -> {
Identity b = value3.apply(Tuple.tuple(in,ina));
return b.flatMap(inb -> {
Identity c = value4.apply(Tuple.tuple(in,ina,inb));
return c;
});
});
});
}
public static Identity forEach(Identity id,
Function super T, ? extends Identity> value2,
Function super Tuple2 super T,? super R1>, ? extends Identity> value3
) {
return id.flatMap(in -> {
Identity a = value2.apply(in);
return a.flatMap(ina -> {
Identity b = value3.apply(Tuple.tuple(in,ina));
return b;
});
});
}
public static Identity forEach(Identity id,
Function super T, ? extends Identity> value2
) {
return id.flatMap(in -> {
Identity a = value2.apply(in);
return a;
});
}
}
}