Constructor and Description |
---|
Instances() |
Modifier and Type | Method and Description |
---|---|
static <T,R> Applicative<Maybe.µ> |
applicative()
import static com.aol.cyclops.hkt.jdk.Maybe.widen;
import static com.aol.cyclops.util.function.Lambda.l1;
import static java.util.Maybe.just;
Maybes.zippingApplicative()
.ap(widen(asMaybe(l1(this::multiplyByTwo))),widen(asMaybe(1,2,3)));
//[2,4,6]
|
static <T> Comonad<Maybe.µ> |
comonad() |
static <T> Foldable<Maybe.µ> |
foldable()
int sum = Maybes.foldable()
.foldLeft(0, (a,b)->a+b, Maybe.widen(Maybe.just(1)));
//1
|
static <T,R> Functor<Maybe.µ> |
functor()
Transform a maybe, mulitplying every element by 2
|
static <T,R> Monad<Maybe.µ> |
monad()
import static com.aol.cyclops.hkt.jdk.Maybe.widen;
Maybe<Integer> maybe = Maybes.monad()
.flatMap(i->widen(MaybeX.range(0,i)), widen(Maybe.just(1,2,3)))
.convert(Maybe::narrowK);
|
static <T> MonadPlus<Maybe.µ> |
monadPlus()
Maybe<Integer> maybe = Maybes.<Integer>monadPlus()
.plus(Maybe.widen(Maybe.just()), Maybe.widen(Maybe.just(10)))
.convert(Maybe::narrowK);
//Maybe.just(10))
|
static <T> MonadPlus<Maybe.µ> |
monadPlus(Monoid<Maybe<T>> m)
Monoid<Maybe<Integer>> m = Monoid.of(Maybe.widen(Maybe.just()), (a,b)->a.isEmpty() ? b : a);
Maybe<Integer> maybe = Maybes.<Integer>monadPlus(m)
.plus(Maybe.widen(Maybe.just(5)), Maybe.widen(Maybe.just(10)))
.convert(Maybe::narrowK);
//Maybe[5]
|
static <T,R> MonadZero<Maybe.µ> |
monadZero()
Maybe<String> maybe = Maybes.unit()
.unit("hello")
.apply(h->Maybes.monadZero().filter((String t)->t.startsWith("he"), h))
.convert(Maybe::narrowK);
//Maybe.just("hello"));
|
static <C2,T> Traverse<Maybe.µ> |
traverse() |
static <T> Pure<Maybe.µ> |
unit()
Maybe<String> maybe = Maybes.unit()
.unit("hello")
.convert(Maybe::narrowK);
//Maybe.just("hello"))
|
public static <T,R> Functor<Maybe.µ> functor()
Maybe<Integer> maybe = Maybes.functor().map(i->i*2, Maybe.widen(Maybe.just(1));
//[2]
An example fluent api working with Maybes
Maybe<Integer> maybe = Maybes.unit()
.unit("hello")
.apply(h->Maybes.functor().map((String v) ->v.length(), h))
.convert(Maybe::narrowK);
public static <T> Pure<Maybe.µ> unit()
Maybe<String> maybe = Maybes.unit()
.unit("hello")
.convert(Maybe::narrowK);
//Maybe.just("hello"))
public static <T,R> Applicative<Maybe.µ> applicative()
import static com.aol.cyclops.hkt.jdk.Maybe.widen;
import static com.aol.cyclops.util.function.Lambda.l1;
import static java.util.Maybe.just;
Maybes.zippingApplicative()
.ap(widen(asMaybe(l1(this::multiplyByTwo))),widen(asMaybe(1,2,3)));
//[2,4,6]
Example fluent API
Maybe<Function<Integer,Integer>> maybeFn =Maybes.unit()
.unit(Lambda.l1((Integer i) ->i*2))
.convert(Maybe::narrowK);
Maybe<Integer> maybe = Maybes.unit()
.unit("hello")
.apply(h->Maybes.functor().map((String v) ->v.length(), h))
.apply(h->Maybes.applicative().ap(maybeFn, h))
.convert(Maybe::narrowK);
//Maybe.just("hello".length()*2))
public static <T,R> Monad<Maybe.µ> monad()
import static com.aol.cyclops.hkt.jdk.Maybe.widen;
Maybe<Integer> maybe = Maybes.monad()
.flatMap(i->widen(MaybeX.range(0,i)), widen(Maybe.just(1,2,3)))
.convert(Maybe::narrowK);
Example fluent API
Maybe<Integer> maybe = Maybes.unit()
.unit("hello")
.apply(h->Maybes.monad().flatMap((String v) ->Maybes.unit().unit(v.length()), h))
.convert(Maybe::narrowK);
//Maybe.just("hello".length())
public static <T,R> MonadZero<Maybe.µ> monadZero()
Maybe<String> maybe = Maybes.unit()
.unit("hello")
.apply(h->Maybes.monadZero().filter((String t)->t.startsWith("he"), h))
.convert(Maybe::narrowK);
//Maybe.just("hello"));
public static <T> MonadPlus<Maybe.µ> monadPlus()
Maybe<Integer> maybe = Maybes.<Integer>monadPlus()
.plus(Maybe.widen(Maybe.just()), Maybe.widen(Maybe.just(10)))
.convert(Maybe::narrowK);
//Maybe.just(10))
public static <T> MonadPlus<Maybe.µ> monadPlus(Monoid<Maybe<T>> m)
Monoid<Maybe<Integer>> m = Monoid.of(Maybe.widen(Maybe.just()), (a,b)->a.isEmpty() ? b : a);
Maybe<Integer> maybe = Maybes.<Integer>monadPlus(m)
.plus(Maybe.widen(Maybe.just(5)), Maybe.widen(Maybe.just(10)))
.convert(Maybe::narrowK);
//Maybe[5]
m
- Monoid to use for combining Maybespublic static <C2,T> Traverse<Maybe.µ> traverse()
public static <T> Foldable<Maybe.µ> foldable()
int sum = Maybes.foldable()
.foldLeft(0, (a,b)->a+b, Maybe.widen(Maybe.just(1)));
//1