cyclops.instances.control.transformers.MaybeTransformer 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.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)));
}
}