cyclops.instances.reactive.collections.mutable.ListXInstances 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.instances.reactive.collections.mutable;
import com.oath.cyclops.hkt.Higher;
import cyclops.arrow.Cokleisli;
import cyclops.arrow.Kleisli;
import cyclops.arrow.MonoidK;
import cyclops.arrow.MonoidKs;
import cyclops.control.Either;
import cyclops.control.Maybe;
import cyclops.control.Option;
import cyclops.data.tuple.Tuple2;
import cyclops.function.Monoid;
import cyclops.hkt.Active;
import cyclops.hkt.Coproduct;
import cyclops.hkt.Nested;
import cyclops.hkt.Product;
import cyclops.reactive.collections.mutable.ListX;
import cyclops.typeclasses.InstanceDefinitions;
import cyclops.typeclasses.Pure;
import cyclops.typeclasses.comonad.Comonad;
import cyclops.typeclasses.foldable.Foldable;
import cyclops.typeclasses.foldable.Unfoldable;
import cyclops.typeclasses.functor.Functor;
import cyclops.typeclasses.monad.Applicative;
import cyclops.typeclasses.monad.Monad;
import cyclops.typeclasses.monad.MonadPlus;
import cyclops.typeclasses.monad.MonadRec;
import cyclops.typeclasses.monad.MonadZero;
import cyclops.typeclasses.monad.Traverse;
import cyclops.typeclasses.monad.TraverseByTraverse;
import lombok.AllArgsConstructor;
import lombok.experimental.UtilityClass;
import lombok.experimental.Wither;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import static com.oath.cyclops.data.ReactiveWitness.list;
@UtilityClass
public class ListXInstances {
public static Kleisli,T> kindKleisli(){
return Kleisli.of(ListXInstances.monad(), ListXInstances::widen);
}
public static Cokleisli> kindCokleisli(){
return Cokleisli.of(ListXInstances::narrowK);
}
public static Nested nested(ListX> nested, InstanceDefinitions def2){
return Nested.of(nested, ListXInstances.definitions(),def2);
}
public static Product product(ListX l, Active active){
return Product.of(allTypeclasses(l),active);
}
public static Coproduct coproduct(ListX l, InstanceDefinitions def2){
return Coproduct.right(l,def2, ListXInstances.definitions());
}
public static Active allTypeclasses(ListX l){
return Active.of(l, ListXInstances.definitions());
}
public static Nested mapM(ListX l,Function super T,? extends Higher> fn, InstanceDefinitions defs){
return Nested.of(l.map(fn), ListXInstances.definitions(), defs);
}
public static InstanceDefinitions definitions(){
return new InstanceDefinitions() {
@Override
public Functor functor() {
return ListXInstances.functor();
}
@Override
public Pure unit() {
return ListXInstances.unit();
}
@Override
public Applicative applicative() {
return ListXInstances.zippingApplicative();
}
@Override
public Monad monad() {
return ListXInstances.monad();
}
@Override
public Option> monadZero() {
return Option.some(ListXInstances.monadZero());
}
@Override
public Option> monadPlus() {
return Option.some(ListXInstances.monadPlus());
}
@Override
public MonadRec monadRec() {
return ListXInstances.monadRec();
}
@Override
public Option> monadPlus(MonoidK m) {
MonadPlus x = ListXInstances.monadPlus(m);
return Maybe.just(x);
}
@Override
public Traverse traverse() {
return ListXInstances.traverse();
}
@Override
public Foldable foldable() {
return ListXInstances.foldable();
}
@Override
public Option> comonad() {
return Maybe.nothing();
}
@Override
public Option> unfoldable() {
return Option.some(ListXInstances.unfoldable());
}
};
}
public static Pure unit() {
return INSTANCE;
}
private final static ListXTypeClasses INSTANCE = new ListXTypeClasses();
@AllArgsConstructor
@Wither
public static class ListXTypeClasses implements MonadPlus,
MonadRec,
TraverseByTraverse,
Foldable,
Unfoldable{
private final MonoidK monoidK;
public ListXTypeClasses(){
monoidK = MonoidKs.listXConcat();
}
@Override
public Higher filter(Predicate super T> predicate, Higher ds) {
return narrowK(ds).filter(predicate);
}
@Override
public Higher> zip(Higher fa, Higher fb) {
return narrowK(fa).zip(narrowK(fb));
}
@Override
public Higher zip(Higher fa, Higher fb, BiFunction super T1, ? super T2, ? extends R> f) {
return narrowK(fa).zip(narrowK(fb),f);
}
@Override
public MonoidK monoid() {
return monoidK;
}
@Override
public Higher flatMap(Function super T, ? extends Higher> fn, Higher ds) {
return narrowK(ds).concatMap(i->narrowK(fn.apply(i)));
}
@Override
public Higher ap(Higher> fn, Higher apply) {
return narrowK(apply)
.zip(narrowK(fn),(a,b)->b.apply(a));
}
@Override
public Higher unit(T value) {
return ListX.of(value);
}
@Override
public Higher map(Function super T, ? extends R> fn, Higher ds) {
return narrowK(ds).map(fn);
}
@Override
public Higher tailRec(T initial, Function super T, ? extends Higher>> fn) {
return ListX.tailRec(initial,i->narrowK(fn.apply(i)));
}
@Override
public Higher> traverseA(Applicative ap, Function super T, ? extends Higher> fn, Higher ds) {
ListX v = narrowK(ds);
return v.>>foldLeft(ap.unit(ListX.empty()),
(a, b) -> ap.zip(fn.apply(b), a, (sn, vec) -> narrowK(vec).plus(sn)));
}
@Override
public R foldMap(Monoid mb, Function super T, ? extends R> fn, Higher ds) {
ListX x = narrowK(ds);
return x.foldLeft(mb.zero(),(a,b)->mb.apply(a,fn.apply(b)));
}
@Override
public Higher> zipWithIndex(Higher ds) {
return narrowK(ds).zipWithIndex();
}
@Override
public T foldRight(Monoid monoid, Higher ds) {
return narrowK(ds).foldRight(monoid);
}
@Override
public T foldLeft(Monoid monoid, Higher ds) {
return narrowK(ds).foldLeft(monoid);
}
@Override
public Higher unfold(T b, Function super T, Option>> fn) {
return ListX.unfold(b,fn);
}
}
public static Unfoldable unfoldable(){
return INSTANCE;
}
public static MonadPlus monadPlus(MonoidK m){
return INSTANCE.withMonoidK(m);
}
public static Applicative zippingApplicative(){
return INSTANCE;
}
public static Functor functor(){
return INSTANCE;
}
public static Monad monad(){
return INSTANCE;
}
public static MonadZero monadZero(){
return INSTANCE;
}
public static MonadPlus monadPlus(){
return INSTANCE;
}
public static MonadRec monadRec(){
return INSTANCE;
}
public static Traverse traverse(){
return INSTANCE;
}
public static Foldable foldable(){
return INSTANCE;
}
public static Higher> widen2(Higher> flux) {
// a functor could be used (if C2 is a functor / one exists for C2 type)
// instead of casting
// cast seems safer as Higher must be a ListX
return (Higher) flux;
}
public static Higher widen(ListX flux) {
return flux;
}
public static ListX narrowK(final Higher future) {
return (ListX) future;
}
public static ListX narrow(final Higher completableList) {
return ((ListX) completableList);//.narrow();
}
}