public class PVectorInstances
extends java.lang.Object
Constructor and Description |
---|
PVectorInstances() |
Modifier and Type | Method and Description |
---|---|
static <T> Foldable<PVectorType.µ> |
foldable()
int sum = PVectors.foldable()
.foldLeft(0, (a,b)->a+b, PVectorType.widen(Arrays.asPVector(1,2,3,4)));
//10
|
static <T,R> Functor<PVectorType.µ> |
functor()
Transform a list, mulitplying every element by 2
|
static <T,R> Monad<PVectorType.µ> |
monad()
import static com.aol.cyclops.hkt.jdk.PVectorType.widen;
PVectorType<Integer> list = PVectors.monad()
.flatMap(i->widen(PVectorX.range(0,i)), widen(Arrays.asPVector(1,2,3)))
.convert(PVectorType::narrowK);
|
static <T> MonadPlus<PVectorType.µ> |
monadPlus()
PVectorType<Integer> list = PVectors.<Integer>monadPlus()
.plus(PVectorType.widen(Arrays.asPVector()), PVectorType.widen(Arrays.asPVector(10)))
.convert(PVectorType::narrowK);
//Arrays.asPVector(10))
|
static <T> MonadPlus<PVectorType.µ> |
monadPlus(com.aol.cyclops.Monoid<PVectorType<T>> m)
Monoid<PVectorType<Integer>> m = Monoid.of(PVectorType.widen(Arrays.asPVector()), (a,b)->a.isEmpty() ? b : a);
PVectorType<Integer> list = PVectors.<Integer>monadPlus(m)
.plus(PVectorType.widen(Arrays.asPVector(5)), PVectorType.widen(Arrays.asPVector(10)))
.convert(PVectorType::narrowK);
//Arrays.asPVector(5))
|
static <T,R> MonadZero<PVectorType.µ> |
monadZero()
PVectorType<String> list = PVectors.unit()
.unit("hello")
.then(h->PVectors.monadZero().filter((String t)->t.startsWith("he"), h))
.convert(PVectorType::narrowK);
//Arrays.asPVector("hello"));
|
static <C2,T> Traverse<PVectorType.µ> |
traverse() |
static <T> Unit<PVectorType.µ> |
unit()
PVectorType<String> list = PVectors.unit()
.unit("hello")
.convert(PVectorType::narrowK);
//Arrays.asPVector("hello"))
|
static <T,R> Applicative<PVectorType.µ> |
zippingApplicative()
import static com.aol.cyclops.hkt.jdk.PVectorType.widen;
import static com.aol.cyclops.util.function.Lambda.l1;
import static java.util.Arrays.asPVector;
PVectors.zippingApplicative()
.ap(widen(asPVector(l1(this::multiplyByTwo))),widen(asPVector(1,2,3)));
//[2,4,6]
|
public static <T,R> Functor<PVectorType.µ> functor()
PVectorType<Integer> list = PVectors.functor().map(i->i*2, PVectorType.widen(Arrays.asPVector(1,2,3));
//[2,4,6]
An example fluent api working with PVectors
PVectorType<Integer> list = PVectors.unit()
.unit("hello")
.then(h->PVectors.functor().map((String v) ->v.length(), h))
.convert(PVectorType::narrowK);
public static <T> Unit<PVectorType.µ> unit()
PVectorType<String> list = PVectors.unit()
.unit("hello")
.convert(PVectorType::narrowK);
//Arrays.asPVector("hello"))
public static <T,R> Applicative<PVectorType.µ> zippingApplicative()
import static com.aol.cyclops.hkt.jdk.PVectorType.widen;
import static com.aol.cyclops.util.function.Lambda.l1;
import static java.util.Arrays.asPVector;
PVectors.zippingApplicative()
.ap(widen(asPVector(l1(this::multiplyByTwo))),widen(asPVector(1,2,3)));
//[2,4,6]
Example fluent API
PVectorType<Function<Integer,Integer>> listFn =PVectors.unit()
.unit(Lambda.l1((Integer i) ->i*2))
.convert(PVectorType::narrowK);
PVectorType<Integer> list = PVectors.unit()
.unit("hello")
.then(h->PVectors.functor().map((String v) ->v.length(), h))
.then(h->PVectors.zippingApplicative().ap(listFn, h))
.convert(PVectorType::narrowK);
//Arrays.asPVector("hello".length()*2))
public static <T,R> Monad<PVectorType.µ> monad()
import static com.aol.cyclops.hkt.jdk.PVectorType.widen;
PVectorType<Integer> list = PVectors.monad()
.flatMap(i->widen(PVectorX.range(0,i)), widen(Arrays.asPVector(1,2,3)))
.convert(PVectorType::narrowK);
Example fluent API
PVectorType<Integer> list = PVectors.unit()
.unit("hello")
.then(h->PVectors.monad().flatMap((String v) ->PVectors.unit().unit(v.length()), h))
.convert(PVectorType::narrowK);
//Arrays.asPVector("hello".length())
public static <T,R> MonadZero<PVectorType.µ> monadZero()
PVectorType<String> list = PVectors.unit()
.unit("hello")
.then(h->PVectors.monadZero().filter((String t)->t.startsWith("he"), h))
.convert(PVectorType::narrowK);
//Arrays.asPVector("hello"));
public static <T> MonadPlus<PVectorType.µ> monadPlus()
PVectorType<Integer> list = PVectors.<Integer>monadPlus()
.plus(PVectorType.widen(Arrays.asPVector()), PVectorType.widen(Arrays.asPVector(10)))
.convert(PVectorType::narrowK);
//Arrays.asPVector(10))
public static <T> MonadPlus<PVectorType.µ> monadPlus(com.aol.cyclops.Monoid<PVectorType<T>> m)
Monoid<PVectorType<Integer>> m = Monoid.of(PVectorType.widen(Arrays.asPVector()), (a,b)->a.isEmpty() ? b : a);
PVectorType<Integer> list = PVectors.<Integer>monadPlus(m)
.plus(PVectorType.widen(Arrays.asPVector(5)), PVectorType.widen(Arrays.asPVector(10)))
.convert(PVectorType::narrowK);
//Arrays.asPVector(5))
m
- Monoid to use for combining PVectorspublic static <C2,T> Traverse<PVectorType.µ> traverse()
public static <T> Foldable<PVectorType.µ> foldable()
int sum = PVectors.foldable()
.foldLeft(0, (a,b)->a+b, PVectorType.widen(Arrays.asPVector(1,2,3,4)));
//10