Skip to content

Instantly share code, notes, and snippets.

@codebje
Created Nov 18, 2015
Embed
What would you like to do?
How a Java 8 Stream acts as a Functor
package au.id.bje.functor;
import java.util.function.Function;
import java.util.stream.Stream;
public class Example {
static class A {}
static class B {}
static class C {}
static B aToB(A a) { return new B(); }
static C bToC(B b) { return new C(); }
static Function<Stream<A>, Stream<B>> streamAToStreamB
= streamA -> streamA.map(Example::aToB);
static Function<Stream<B>, Stream<C>> streamBToStreamC
= streamA -> streamA.map(Example::bToC);
static Function<Stream<A>, Stream<C>> streamAtoStreamC
= streamA -> streamBToStreamC.apply(streamAToStreamB.apply(streamA));
static Function<Stream<A>, Stream<C>> streamAtoStreamC2
= streamA -> streamA.map(a -> bToC(aToB(a)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment