Created
November 18, 2015 04:30
-
-
Save codebje/f059af961579de353ccc to your computer and use it in GitHub Desktop.
How a Java 8 Stream acts as a Functor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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