Skip to content

Instantly share code, notes, and snippets.

@codebje
Created November 18, 2015 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebje/f059af961579de353ccc to your computer and use it in GitHub Desktop.
Save codebje/f059af961579de353ccc to your computer and use it in GitHub Desktop.
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