Skip to content

Commit

Permalink
comonad
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmcclean committed Nov 12, 2016
1 parent 7eed8f7 commit 8e420b6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ static <CRE,A,B> SupplierMonadPlus<CRE,A,B> monadPlus(MonadZero<CRE> monad,
}
@AllArgsConstructor
static class GeneralComonad<CRE,A,B> implements Comonad<CRE>{
GeneralFunctor<CRE,A,B> functor;
GeneralUnit<CRE,A> unit;
Functor<CRE> functor;
Unit<CRE> unit;
Function<? super Higher<CRE, A>, ? extends A> extractFn;
<T> Function<? super Higher<CRE, T>, ? extends T> extractFn(){
return (Function)extractFn;
Expand All @@ -340,7 +340,7 @@ public <T> T extract(Higher<CRE, T> ds) {
}

}
static <CRE,T,R> GeneralComonad<CRE,T,R> comonad(GeneralFunctor<CRE,T,R> functor, GeneralUnit<CRE,T> unit,
static <CRE,T,R> GeneralComonad<CRE,T,R> comonad(Functor<CRE> functor, Unit<CRE> unit,
Function<? super Higher<CRE, T>, ? extends T> extractFn ) {

return new GeneralComonad<>(functor,unit,extractFn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.aol.cyclops.control.Eval;
import com.aol.cyclops.hkt.alias.Higher;
import com.aol.cyclops.hkt.cyclops.EvalType;
import com.aol.cyclops.hkt.cyclops.FutureType;
import com.aol.cyclops.hkt.instances.General;
import com.aol.cyclops.hkt.typeclasses.Unit;
import com.aol.cyclops.hkt.typeclasses.comonad.Comonad;
import com.aol.cyclops.hkt.typeclasses.foldable.Foldable;
import com.aol.cyclops.hkt.typeclasses.functor.Functor;
import com.aol.cyclops.hkt.typeclasses.monad.Applicative;
Expand Down Expand Up @@ -241,7 +243,10 @@ public class Evals {
return General.foldable(foldRightFn, foldLeftFn);
}


public static <T> Comonad<EvalType.µ> comonad(){
Function<? super Higher<EvalType.µ, T>, ? extends T> extractFn = maybe -> maybe.convert(EvalType::narrow).get();
return General.comonad(functor(), unit(), extractFn);
}
private <T> EvalType<T> of(T value){
return EvalType.widen(Eval.now(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import com.aol.cyclops.control.FutureW;
import com.aol.cyclops.hkt.alias.Higher;
import com.aol.cyclops.hkt.cyclops.FutureType;
import com.aol.cyclops.hkt.cyclops.MaybeType;
import com.aol.cyclops.hkt.instances.General;
import com.aol.cyclops.hkt.typeclasses.Unit;
import com.aol.cyclops.hkt.typeclasses.comonad.Comonad;
import com.aol.cyclops.hkt.typeclasses.foldable.Foldable;
import com.aol.cyclops.hkt.typeclasses.functor.Functor;
import com.aol.cyclops.hkt.typeclasses.monad.Applicative;
Expand Down Expand Up @@ -241,7 +243,10 @@ public class FutureWs {
BiFunction<Monoid<T>,Higher<FutureType.µ,T>,T> foldLeftFn = (m,l)-> m.apply(m.zero(), FutureType.narrow(l).get());
return General.foldable(foldRightFn, foldLeftFn);
}

public static <T> Comonad<FutureType.µ> comonad(){
Function<? super Higher<FutureType.µ, T>, ? extends T> extractFn = maybe -> maybe.convert(FutureType::narrow).get();
return General.comonad(functor(), unit(), extractFn);
}

private <T> FutureType<T> of(T value){
return FutureType.widen(FutureW.ofResult(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.aol.cyclops.hkt.cyclops.MaybeType;
import com.aol.cyclops.hkt.instances.General;
import com.aol.cyclops.hkt.typeclasses.Unit;
import com.aol.cyclops.hkt.typeclasses.comonad.Comonad;
import com.aol.cyclops.hkt.typeclasses.foldable.Foldable;
import com.aol.cyclops.hkt.typeclasses.functor.Functor;
import com.aol.cyclops.hkt.typeclasses.monad.Applicative;
Expand Down Expand Up @@ -241,6 +242,11 @@ public class Maybes {
BiFunction<Monoid<T>,Higher<MaybeType.µ,T>,T> foldLeftFn = (m,l)-> MaybeType.narrow(l).orElse(m.zero());
return General.foldable(foldRightFn, foldLeftFn);
}

public static <T> Comonad<MaybeType.µ> comonad(){
Function<? super Higher<MaybeType.µ, T>, ? extends T> extractFn = maybe -> maybe.convert(MaybeType::narrow).get();
return General.comonad(functor(), unit(), extractFn);
}


private <T> MaybeType<T> of(T value){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import com.aol.cyclops.Monoid;
import com.aol.cyclops.Monoids;
import com.aol.cyclops.hkt.alias.Higher;
import com.aol.cyclops.hkt.cyclops.MaybeType;
import com.aol.cyclops.hkt.instances.General;
import com.aol.cyclops.hkt.jdk.CompletableFutureType;
import com.aol.cyclops.hkt.typeclasses.Unit;
import com.aol.cyclops.hkt.typeclasses.comonad.Comonad;
import com.aol.cyclops.hkt.typeclasses.foldable.Foldable;
import com.aol.cyclops.hkt.typeclasses.functor.Functor;
import com.aol.cyclops.hkt.typeclasses.monad.Applicative;
Expand Down Expand Up @@ -240,7 +242,10 @@ public class CompletableFutureInstances {
BiFunction<Monoid<T>,Higher<CompletableFutureType.µ,T>,T> foldLeftFn = (m,l)-> m.apply(m.zero(), CompletableFutureType.narrow(l).join());
return General.foldable(foldRightFn, foldLeftFn);
}

public static <T> Comonad<CompletableFutureType.µ> comonad(){
Function<? super Higher<CompletableFutureType.µ, T>, ? extends T> extractFn = maybe -> maybe.convert(CompletableFutureType::narrow).join();
return General.comonad(functor(), unit(), extractFn);
}

private <T> CompletableFutureType<T> of(T value){
return CompletableFutureType.widen(CompletableFuture.completedFuture(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.aol.cyclops.hkt.instances.General;
import com.aol.cyclops.hkt.jdk.OptionalType;
import com.aol.cyclops.hkt.typeclasses.Unit;
import com.aol.cyclops.hkt.typeclasses.comonad.Comonad;
import com.aol.cyclops.hkt.typeclasses.foldable.Foldable;
import com.aol.cyclops.hkt.typeclasses.functor.Functor;
import com.aol.cyclops.hkt.typeclasses.monad.Applicative;
Expand Down Expand Up @@ -242,7 +243,10 @@ public class OptionalInstances {
BiFunction<Monoid<T>,Higher<OptionalType.µ,T>,T> foldLeftFn = (m,l)-> OptionalType.narrow(l).orElse(m.zero());
return General.foldable(foldRightFn, foldLeftFn);
}

public static <T> Comonad<OptionalType.µ> comonad(){
Function<? super Higher<OptionalType.µ, T>, ? extends T> extractFn = maybe -> maybe.convert(OptionalType::narrow).get();
return General.comonad(functor(), unit(), extractFn);
}

private <T> OptionalType<T> of(T value){
return OptionalType.widen(Optional.of(value));
Expand Down

0 comments on commit 8e420b6

Please sign in to comment.