All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
cyclops.instances.control.MaybeInstances Maven / Gradle / Ivy
Go to download
Platform for Functional Reactive Programming with Java 8
package cyclops.instances.control;
import com.oath.cyclops.hkt.DataWitness;
import com.oath.cyclops.hkt.DataWitness.option;
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.kinds.OptionalKind;
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 java.util.function.Function;
/**
* Companion class for creating Type Class instances for working with Maybes
* @author johnmcclean
*
*/
@UtilityClass
public class MaybeInstances {
public static Maybe fromOptionalKind(final OptionalKind opt){
return fromOptional(OptionalKind.narrow(opt));
}
public static Kleisli,T> kindKleisli(){
return Kleisli.of(MaybeInstances.monad(), Maybe::widen);
}
public static Cokleisli> kindCokleisli(){
return Cokleisli.of(Maybe::narrowK);
}
public static Nested nested(Maybe> nested, InstanceDefinitions def2){
return Nested.of(nested, MaybeInstances.definitions(),def2);
}
public static Product product(Maybe m, Active active){
return Product.of(allTypeclasses(m),active);
}
public static Coproduct coproduct(Maybe m, InstanceDefinitions def2){
return Coproduct.right(m,def2, MaybeInstances.definitions());
}
public static Active allTypeclasses(Maybe m){
return Active.of(m, MaybeInstances.definitions());
}
public static Nested mapM(Maybe m,Function super T,? extends Higher> fn, InstanceDefinitions defs){
return Nested.of(m.map(fn), MaybeInstances.definitions(), defs);
}
/**
* Construct an equivalent Maybe from the Supplied Optional
*
* {@code
* MaybeType some = MaybeType.fromOptional(Optional.of(10));
* //Maybe[10], Some[10]
*
* MaybeType none = MaybeType.fromOptional(Optional.zero());
* //Maybe.zero, None[]
* }
*
*
* @param optional Optional to construct Maybe from
* @return Maybe created from Optional
*/
public static Maybe fromOptional(Higher optional){
return Maybe.fromOptional(OptionalKind.narrowK(optional));
}
public static InstanceDefinitions definitions(){
return new InstanceDefinitions () {
@Override
public Functor functor() {
return MaybeInstances.functor();
}
@Override
public Pure unit() {
return MaybeInstances.unit();
}
@Override
public Applicative applicative() {
return MaybeInstances.applicative();
}
@Override
public Monad monad() {
return MaybeInstances.monad();
}
@Override
public Option> monadZero() {
return Option.some(MaybeInstances.monadZero());
}
@Override
public Option> monadPlus() {
return Option.some(MaybeInstances.monadPlus());
}
@Override
public MonadRec monadRec() {
return MaybeInstances.monadRec();
}
@Override
public Option> monadPlus(MonoidK m) {
return Option.some(MaybeInstances.monadPlus(m));
}
@Override
public Traverse traverse() {
return MaybeInstances.traverse();
}
@Override
public Foldable foldable() {
return MaybeInstances.foldable();
}
@Override
public Option> comonad() {
return Maybe.nothing();
}
@Override
public Option> unfoldable() {
return Maybe.nothing();
}
};
}
private final OptionInstances.OptionTypeclasses INSTANCE = new OptionInstances.OptionTypeclasses();
@AllArgsConstructor
public static class OptionTypeclasses implements MonadPlus,
MonadRec ,
TraverseByTraverse ,
Foldable ,
Unfoldable {
@Override
public T foldRight(Monoid monoid, Higher ds) {
return Maybe.narrowK(ds).fold(monoid);
}
@Override
public T foldLeft(Monoid monoid, Higher ds) {
return Maybe.narrowK(ds).fold(monoid);
}
@Override
public Higher unfold(T b, Function super T, Option>> fn) {
return Maybe.fromPublisher(fn.apply(b).map(t->t._1()));
}
@Override
public MonoidK monoid() {
return MonoidKs.firstPresentMaybe();
}
@Override
public Higher flatMap(Function super T, ? extends Higher > fn, Higher ds) {
return Maybe.narrowK(ds).flatMap(t-> Option.narrowK(fn.apply(t)));
}
@Override
public Higher> traverseA(Applicative applicative, Function super T, ? extends Higher> fn, Higher ds) {
Maybe maybe = Maybe.narrowK(ds);
return maybe.fold(some-> applicative.map(m->Option.of(m), fn.apply(some)),
()->applicative.unit(Option.none()));
}
@Override
public R foldMap(Monoid mb, Function super T, ? extends R> fn, Higher ds) {
Maybe opt = Maybe.narrowK(ds).map(fn);
return opt.fold(mb);
}
@Override
public Higher ap(Higher > fn, Higher apply) {
return Maybe.narrowK(apply).zip(Option.narrowK(fn), (a, b)->b.apply(a));
}
@Override
public Higher unit(T value) {
return Maybe.just(value);
}
@Override
public Higher map(Function super T, ? extends R> fn, Higher ds) {
return Maybe.narrowK(ds).map(fn);
}
@Override
public Higher tailRec(T initial, Function super T, ? extends Higher >> fn) {
return Maybe.tailRec(initial,t-> Maybe.narrowK(fn.apply(t)));
}
}
public static Functor functor(){
return INSTANCE;
}
public static Pure unit(){
return INSTANCE;
}
public static Applicative applicative(){
return INSTANCE;
}
public static Monad monad(){
return INSTANCE;
}
public static MonadRec monadRec(){
return INSTANCE;
}
public static MonadZero monadZero(){
return INSTANCE;
}
public static MonadPlus monadPlus(){
return INSTANCE;
}
public static MonadPlus monadPlus(MonoidK m){
return INSTANCE;
}
public static Traverse traverse(){
return INSTANCE;
}
public static Foldable foldable(){
return INSTANCE;
}
}