Top Banner
Javaz Javaz Functional design Functional design in Java 8 in Java 8
53

Javaz. Functional design in Java 8.

Aug 21, 2015

Download

Software

Dubs Vadim
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Javaz. Functional design in Java 8.

JavazJavazFunctional designFunctional design

in Java 8in Java 8

Page 2: Javaz. Functional design in Java 8.

IntroIntro

All Java code can be found at Javazhttps://github.com/escalate42/Javaz

The emergence of native support for lambdas in Java 8has opened the possibility to adapt the rich experienceof statically typed functional programming languages tothe Java world. So the Javaz appears.

finally!This presentation can be found athttps://slides.com/vadimdubs/javaz

Page 3: Javaz. Functional design in Java 8.

Common functionalCommon functionalprogramming patternsprogramming patterns

FunctorApplicativeFunctorMonad

First appeared in Haskell programing langugage

Page 4: Javaz. Functional design in Java 8.

TypeclassesTypeclasses

First appeared in Haskell programing langugageIs a sort of interface that defines some behaviorYou can think of them kind of as Java interfaces

Page 5: Javaz. Functional design in Java 8.
Page 6: Javaz. Functional design in Java 8.

-- Typeclass definition with name Eq, type variable a-- and function == which has two params of type a and-- returns Boolclass Eq a where eq :: a -> a -> Bool

-- Data Type definition (like Java enum in this case)data TrafficLight = Red | Yellow | Green

-- Instance definition for TrafficLight data type and-- Eq typeclassinstance Eq TrafficLight where eq Red Red = True eq Yellow Yellow = True eq Green Green = True eq _ _ = False

Page 7: Javaz. Functional design in Java 8.

public interface Eq<A> { public boolean eq(A other);}

public final class TrafficLight implements Eq<TrafficLight> { public static TrafficLight RED = new TrafficLight(); public static TrafficLight YELLOW = new TrafficLight(); public static TrafficLight GREEN = new TrafficLight(); private TrafficLight() {} @Override public boolean eq(TrafficLight other) { return other == this; }}

Page 8: Javaz. Functional design in Java 8.

FunctionFunction

@FunctionalInterfaceinterface Function<A, B> { B apply(A a);}

Function<A, B> function1 = Functions::function;Function<A, B> function2 = a -> new B(a);

Page 9: Javaz. Functional design in Java 8.

function :: a -> b

Page 10: Javaz. Functional design in Java 8.

@FunctionalInterfaceinterface Function<A, B> { B apply(A a);}

Function<A, B> f;Function<B, C> g;

static <A, B, C> Function<A, C> compose( Function<A, B> ab, Function<B, C> bc) { return a -> bc.apply(ab.apply(a));}

Function<A, C> h = compose(f, g);

Page 11: Javaz. Functional design in Java 8.

f :: a -> bg :: b -> c

. :: (a -> b) -> (b -> c) -> a -> cf1 . f2 = \x -> f2 (f1 x)

h :: a -> ch = f . g

Page 12: Javaz. Functional design in Java 8.

FunctorFunctorfor things that can be mapped over.

class Functor F where fmap :: (a -> b) -> F a -> F b

Page 13: Javaz. Functional design in Java 8.
Page 14: Javaz. Functional design in Java 8.
Page 15: Javaz. Functional design in Java 8.
Page 16: Javaz. Functional design in Java 8.

data Maybe a = Just a | Nothing

Page 17: Javaz. Functional design in Java 8.
Page 18: Javaz. Functional design in Java 8.
Page 19: Javaz. Functional design in Java 8.
Page 20: Javaz. Functional design in Java 8.
Page 21: Javaz. Functional design in Java 8.
Page 22: Javaz. Functional design in Java 8.
Page 23: Javaz. Functional design in Java 8.

FunctorFunctorfor things that can be mapped over.

class Functor F where fmap :: (a -> b) -> F a -> F b

Page 24: Javaz. Functional design in Java 8.

Applicative FunctorApplicative Functor

class (Functor F) => Applicative F where pure :: a -> F a (<*>) :: F (a -> b) -> F a -> F b

Page 25: Javaz. Functional design in Java 8.
Page 26: Javaz. Functional design in Java 8.
Page 27: Javaz. Functional design in Java 8.
Page 28: Javaz. Functional design in Java 8.
Page 29: Javaz. Functional design in Java 8.

Applicative FunctorApplicative Functor

class (Functor F) => Applicative F where pure :: a -> F a (<*>) :: F (a -> b) -> F a -> F b

Page 30: Javaz. Functional design in Java 8.

MonadMonad

class Monad M where return :: a -> M a (>>=) :: M a -> (a -> M b) -> M b

Page 31: Javaz. Functional design in Java 8.
Page 32: Javaz. Functional design in Java 8.
Page 33: Javaz. Functional design in Java 8.
Page 34: Javaz. Functional design in Java 8.
Page 35: Javaz. Functional design in Java 8.
Page 36: Javaz. Functional design in Java 8.
Page 37: Javaz. Functional design in Java 8.

MonadMonad

class Monad M where return :: a -> M a (>>=) :: M a -> (a -> M b) -> M b

Page 38: Javaz. Functional design in Java 8.

f :: a -> M ag :: a -> M b

-- Bind function(>>=) :: M a -> (a -> M b) -> M b

\a -> (f a) >>= \a -> (g a)

-- Same in term of types-- (>>=) is the same as-- composition of functions(a -> M a) >>= (a -> M b)

-- Composition of functions(a -> a) . (a -> b)

Page 39: Javaz. Functional design in Java 8.
Page 40: Javaz. Functional design in Java 8.

CommonCommonimplementationsimplementations

Collection - container for a group ofvaluesOption/Maybe - for optional valuesEither - for results that either success orfailureFuture - for async computations

Page 41: Javaz. Functional design in Java 8.

Option Option

For optional values, typesafe way to avoid null and null-checks

Option<T> always is in one of two states:

Some<T> - simple container for value of type TNone<T> - represents absence of any value of type T

Javaz implementation

Page 42: Javaz. Functional design in Java 8.

User auth(String l, String p);Role getRole(User u);Permissions getPermissions(Role r);List<Partner> getPartners(Permissions p);

User user = auth("user", "password");Role role = null;Permissions permissions = null;List<Partner> partners = new ArrayList<>();if (user != null) { role = getRole(user);}if (role != null) { permissions = getPermissions(role);}if (permissions != null) { partners.addAll(getPartners(permissions));}

Page 43: Javaz. Functional design in Java 8.

Function2<String, String, Option<User>> auth;Function<User, Option<Role>> getRole;Function<Role, Option<Permissions>> getPermissions;Function<Permissions, List<Partner>> getPartners;

List<Partner> partners = // trying to authenticate user auth.apply("login", "password") // trying to get Role for this // user from service via http .flatMap(getRole) // the same as >>= // trying to load permissions // from database .flatMap(getPermissions) // trying to load partners from // another data source .map(getPartners) // the same as fmap .getOrElse(new ArrayList());

Page 44: Javaz. Functional design in Java 8.

OptionalOptionalimport static Optional.of;import static Optional.empty;final Optional<Integer> first = of(3);final Optional<Integer> second = of(4);final Optional<Integer> empty = empty();

// Optional is a functor and monadfirst.map(i -> i * i) // Some(9)empty.map(i -> i * i) // None

first.flatMap(f -> second.map(s -> f + s));// Some(7)first.flatMap(f -> empty.map(s -> f + s));// None

Implementation from standard Java 8 library

Page 45: Javaz. Functional design in Java 8.

EitherEither

For results that either success or failure, typesafe way to avoidusege of exceptions.Has no analogs in standard Java library.

Either<L, R> always is in one of two states:

Right<L, R> - container for value of type RLeft<L, R> - container for values of type L that represents somefailure

Javaz implementation

Page 46: Javaz. Functional design in Java 8.

F2<String, String, Either<ErrorInfo, User>> auth;F<User, Either<ErrorInfo, Role>> getRole;F<Role, Either<ErrorInfo, Permissions>> getPermissions;F<Permissions, List<Partner>> getPartners;

Either<ErrorInfo, List<Partner>> eitherPartners = // trying to authenticate user auth.apply("login", "password") // trying to get Role for this // user from service via http .fmap(getRole) // trying to load permissions // from database .fmap(getPermissions) // trying to load partners from // another data source .map(getPartners);

eitherPartners.mapLeft(logger::error);List<String> partnerNames = eitherPartners.foldRight( new ArrayList(), partner -> partner.getName)

Page 47: Javaz. Functional design in Java 8.

StreamStream

final Stream<Integer> stream = Arrays.asList(1, 2, 3, 4, 5).stream();

// Streams are functorsstream.map(i -> i + 1);// [2, 3, 4, 5, 6]stream.forEach(System.out::print);// out > 12345

Implementation from standard Java 8 library

Page 48: Javaz. Functional design in Java 8.

StreamStream

final Stream<Integer> stream = Arrays.asList(1, 2, 3, 4, 5).stream();

// Streams are monadsstream.flatMap( i -> Arrays.asList(i + 1, i + 2).stream());// [2, 3, 3, 4, 4, 5, 5, 6, 6, 7]

Implementation from standard Java 8 library

Page 49: Javaz. Functional design in Java 8.

StreamStream

Function<User, Stream<Permission>> permissionsByUser;Function<Permission, Stream<Partner>> partnersByPermissions;

Stream<User> users = Arrays.asList(user1, user2).stream();

Set<Partners> availablePartners = users .flatMap(permissionByUser) // get all permissions of user 1 and user2 .distinct() // left only unique items .flatMap(partnersByPermissions) // get all partners available through permissions .collect(Collectors.toSet);

Implementation from standard Java 8 library

Page 50: Javaz. Functional design in Java 8.

FutureFutureJavaz implementation

Function2<String, String, Future<User>> auth;Function<User, Future<Stream<User>>> getFriends;Function<User, Stream<Group>> getGroups;

Future<User> user = auth.apply("login", "password");Future<Stream<Group>> myGroups = user.map(getGroups)Future<Stream<User>> friendsGroups = user .flatMap(getFriends) .map(Stream::flatMap(getGroups).distinct());

Future<Set<Group>> uniqueFriendsGroups = yieldFor( myGroups, myFirends, (myGroups, firendsGroups) -> friendsGroups.collect(toSet) .removeAll(myGroups.collect(toSet)))uniqueFriendsGroups.get(100, TimeUnit.MILLISECONDS)

Page 51: Javaz. Functional design in Java 8.

CompletableFutureCompletableFutureImplementation from standard Java 8 library

final CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 3 * 2);

// CompletableFuture is functorfuture.thenApplyAsync(i -> i * i); // CompletableFuture(36)future.handleAsync( (val, exc) -> val != null ? val.toString() : ""); // CompletableFuture("6")future.thenAcceptAsync(System.out::println); // out > 6

// CompletableFuture is monadfuture.thenComposeAsync( i -> CompletableFuture.supplyAsync(() -> i * 2)); // CompletableFuture(12)

Page 52: Javaz. Functional design in Java 8.

Why should I care?Why should I care?

Simple complexityComposabilityType safetyUnification

Page 53: Javaz. Functional design in Java 8.