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