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

cyclops.instances.control.transformers.MaybeTransformer Maven / Gradle / Ivy

The newest version!
package cyclops.instances.control.transformers;

import com.oath.cyclops.hkt.DataWitness.option;
import com.oath.cyclops.hkt.Higher;
import cyclops.control.Maybe;
import cyclops.hkt.Nested;
import cyclops.transformers.Transformer;
import cyclops.transformers.TransformerFactory;
import cyclops.typeclasses.monad.Monad;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

import java.util.function.Function;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class MaybeTransformer implements Transformer {
  private final Nested nested;
  private final Monad monad1;

  private final  static  TransformerFactory factory(){
    return MaybeTransformer::maybeT;
  }
  public static  MaybeTransformer maybeT(Nested nested){
    return new MaybeTransformer(nested,nested.def1.monad());
  }
  @Override
  public  Nested flatMap(Function> fn) {
    Higher> r = monad1.flatMap(m -> Maybe.narrowK(m).fold(t -> fn.apply(t).nested,
      () -> monad1.unit(Maybe.nothing())),
      nested.nested);



    return Nested.of(r, nested.def1, nested.def2);



  }

  @Override
  public  Nested flatMapK(Function>> fn) {
    return flatMap(fn.andThen(x->Nested.of(x,nested.def1,nested.def2)));
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy