All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cyclops.instances.control.MaybeInstances Maven / Gradle / Ivy

There is a newer version: 10.4.1
Show newest version
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> 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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy