78
11/03/2017 1 Javaslang Achieve functional eloquence in Java Blagoj Atanasovski | dev@Sorsix

Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

11/03/2017 1

JavaslangAchieve functional eloquence in Java

Blagoj Atanasovski | dev@Sorsix

Page 2: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

What I was taught functional programming is:(defun add-two-lists (a b c &optional (d c)) (if a (add-two-lists (cdr a) (cdr b) (cdr (rplaca c (+ (car a) (car b)))) d) d))

(add-two-lists '(1 2 3 4 5) '(1 2 3 4 5) '(nil nil nil nil nil))

11/03/20172

Page 3: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why functional programming?class SomeClass { private boolean magicFlag;

public boolean isMagicFlag() { return magicFlag; } public void updateSomething() { this.magicFlag = true;} public int doSomething() { return magicFlag ? 1 : 0; }}

11/03/20173

Page 4: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why functional programming?class SomeClass { private boolean magicFlag;

public boolean isMagicFlag() { return magicFlag; } public void updateSomething() { this.magicFlag = true;} public int doSomething() { return magicFlag ? 1 : 0; }}

What will doSomething() return?

11/03/20174

Page 5: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why functional programming?● What kind of input am I getting?

11/03/20175

Page 6: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why functional programming?● What kind of input am I getting?

○ Is it mutable?

11/03/20176

Page 7: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why functional programming?● What kind of input am I getting?

○ Is it mutable?○ Will someone outside of my code modify it?

11/03/20177

Page 8: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why functional programming?● What kind of input am I getting?

○ Is it mutable?○ Will someone outside of my code modify it?○ Can I mutate it?

11/03/20178

Page 9: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why functional programming?● What kind of input am I getting?

○ Is it mutable?○ Will someone outside of my code modify it?○ Can I mutate it?○ Am I mutating it without knowing?

11/03/20179

Page 10: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why functional programming?● What kind of input am I getting?

○ Is it mutable?○ Will someone outside of my code modify it?○ Can I mutate it?○ Am I mutating it without knowing?

● How do I synchronize my code?

11/03/201710

Page 11: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why functional programming?● What kind of input am I getting?

○ Is it mutable?○ Will someone outside of my code modify it?○ Can I mutate it?○ Am I mutating it without knowing?

● How do I synchronize my code?○ Is the input thread safe?

11/03/201711

Page 12: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why functional programming?● What kind of input am I getting?

○ Is it mutable?○ Will someone outside of my code modify it?○ Can I mutate it?○ Am I mutating it without knowing?

● How do I synchronize my code?○ Is the input thread safe?○ How can I be sure no race conditions occur?

11/03/201712

Page 13: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Side-effects● It’s fairly easy to write code in Java with side-effects

○ changing objects or variables in place○ printing to the console○ writing to a log file or to a database

11/03/201713

Page 14: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Side-effects● It’s fairly easy to write code in Java with side-effects

○ changing objects or variables in place○ printing to the console○ writing to a log file or to a database

● Not all side-effects are harmful● Side-effects are considered harmful if they affect the semantics of our

program in an undesirable way.

11/03/201714

Page 15: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Side-effects● It’s fairly easy to write code in Java with side-effects

○ changing objects or variables in place○ printing to the console○ writing to a log file or to a database

● Not all side-effects are harmful● Side-effects are considered harmful if they affect the semantics of our

program in an undesirable way.

● If a function throws an exception => side-effect that affects our program○ Exceptions are like non-local goto-statements○ They break normal control-flow

11/03/201715

Page 16: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Side-effects● Real-world applications do perform side-effects.

int divide(int dividend, int divisor) { return dividend / divisor;}

11/03/201716

Page 17: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Side-effects● Real-world applications do perform side-effects

int divide(int dividend, int divisor) { return dividend / divisor;}

● Lets’ modify it a bit

Try<Integer> divide(int dividend, int divisor) { return Try.of(() -> dividend / divisor);}

11/03/201717

Page 18: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Side-effects● Real-world applications do perform side-effects

int divide(int dividend, int divisor) { return dividend / divisor;}

● Lets’ modify it a bit

Try<Integer> divide(int dividend, int divisor) { return Try.of(() -> dividend / divisor);}

● This version of divide does not throw any exception anymore. ● We made the possible failure explicit by using the type Try.

11/03/201718

Page 19: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Referential Transparency● A function/expression, is called referentially transparent if a call can be

replaced by its value without affecting the behavior of the program.

11/03/201719

Page 20: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Referential Transparency● A function/expression, is called referentially transparent if a call can be

replaced by its value without affecting the behavior of the program. ● Given the same input the output is always the same.

Math.random(); Math.max(1, 2);

11/03/201720

Page 21: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Referential Transparency● A function/expression, is called referentially transparent if a call can be

replaced by its value without affecting the behavior of the program. ● Given the same input the output is always the same.

Math.random(); Math.max(1, 2);

● A function is called pure if all expressions involved are referentially transparent.

11/03/201721

Page 22: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Referential Transparency● A function/expression, is called referentially transparent if a call can be

replaced by its value without affecting the behavior of the program. ● Given the same input the output is always the same.

Math.random(); Math.max(1, 2);

● A function is called pure if all expressions involved are referentially transparent.

● An application composed of pure functions will most probably just work if it compiles.

● We are able to reason about it. Unit tests are easy to write and debugging becomes a relict of the past.

11/03/201722

Page 23: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Immutable ValuesThe key to better Java code is to use immutable values paired with referentially transparent functions.

11/03/201723

Page 24: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Immutable ValuesThe key to better Java code is to use immutable values paired with referentially transparent functions.

Immutable values are:● Inherently thread-safe

11/03/201724

Page 25: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Immutable ValuesThe key to better Java code is to use immutable values paired with referentially transparent functions.

Immutable values are:● Inherently thread-safe

○ do not need to be synchronized

11/03/201725

Page 26: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Immutable ValuesThe key to better Java code is to use immutable values paired with referentially transparent functions.

Immutable values are:● Inherently thread-safe

○ do not need to be synchronized● Are stable regarding equals and hashCode

11/03/201726

Page 27: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Immutable ValuesThe key to better Java code is to use immutable values paired with referentially transparent functions.

Immutable values are:● Inherently thread-safe

○ do not need to be synchronized● Are stable regarding equals and hashCode

○ are reliable hash keys

11/03/201727

Page 28: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Immutable ValuesThe key to better Java code is to use immutable values paired with referentially transparent functions.

Immutable values are:● Inherently thread-safe

○ do not need to be synchronized● Are stable regarding equals and hashCode

○ are reliable hash keys● Do not need to be cloned

11/03/201728

Page 29: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why we need Javaslang● Java 8 brought a lot of changes, but

11/03/201729

Page 30: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why we need Javaslang● Java 8 brought a lot of changes, but

○ No functional data structures

11/03/201730

Page 31: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why we need Javaslang● Java 8 brought a lot of changes, but

○ No functional data structures○ No currying, partial application, memoization, lifting

11/03/201731

Page 32: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why we need Javaslang● Java 8 brought a lot of changes, but

○ No functional data structures○ No currying, partial application, memoization, lifting○ No Tuples

11/03/201732

Page 33: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why we need Javaslang● Java 8 brought a lot of changes, but

○ No functional data structures○ No currying, partial application, memoization, lifting○ No Tuples○ Lack of Stream/Optional in existing APIs

11/03/201733

Page 34: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why we need Javaslang● Java 8 brought a lot of changes, but

○ No functional data structures○ No currying, partial application, memoization, lifting○ No Tuples○ Lack of Stream/Optional in existing APIs○ No checked exceptions in lambdas

11/03/201734

Page 35: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why we need Javaslang● Java 8 brought a lot of changes, but

○ No functional data structures○ No currying, partial application, memoization, lifting○ No Tuples○ Lack of Stream/Optional in existing APIs○ No checked exceptions in lambdas○ Failure handling (Try, Either)

11/03/201735

Page 36: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why we need Javaslang● Java 8 brought a lot of changes, but

○ No functional data structures○ No currying, partial application, memoization, lifting○ No Tuples○ Lack of Stream/Optional in existing APIs○ No checked exceptions in lambdas○ Failure handling (Try, Either)○ list.stream().map(...).collect(toList())

11/03/201736

Page 37: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Why we need Javaslang● Java 8 brought a lot of changes, but

○ No functional data structures○ No currying, partial application, memoization, lifting○ No Tuples○ Lack of Stream/Optional in existing APIs○ No checked exceptions in lambdas○ Failure handling (Try, Either)○ list.stream().map(...).collect(toList())○ ...

● Javaslang was created by Daniel Dietrich and first released in 2013. It leverages Java 8’s lambdas to create various new features based on functional patterns

11/03/201737

Page 38: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Does Java have immutable collections?List<String> underlying = new ArrayList<>();underlying.add("1","2");List<String> list = Collections.unmodifiableList(underlying);

11/03/201738

Page 39: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Does Java have immutable collections?List<String> underlying = new ArrayList<>();underlying.add("1","2");List<String> list = Collections.unmodifiableList(underlying);

underlying.add("3");assert list.size() != underlying.size(); // What will happen?

11/03/201739

Page 40: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Functional Data Structuresjavaslang.collection.List<User> users = List.of( new User("1", "1@mail"), new User("2", "2@mail"));

// users is immutableusers.push(new User("3", "3@email"));assert users.size() == 2; // It will pass

11/03/201740

Page 41: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Functional Data Structuresjavaslang.collection.List<User> users = List.of( new User("1", "1@mail"), new User("2", "2@mail"));

// users is immutableusers.push(new User("3", "3@email"));assert users.size() == 2; // It will pass

users = users.push(new User("3", "3@email"));

users .map(User::getEmail) .toSet() .forEach(emailService::sendWelcomeEmailTo);

11/03/201741

Page 42: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Partial application

// (template, user) => ContentsFunction2<String, User, String> emailTxt =

(template,user) -> template.replace("_user_", user.getName());

11/03/201742

Page 43: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Partial application

// (template, user) => ContentsFunction2<String, User, String> emailTxt =

(template,user) -> template.replace("_user_", user.getName());

String emailTemplate = "Hello _user_";

11/03/201743

Page 44: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Partial application

// (template, user) => ContentsFunction2<String, User, String> emailTxt =

(template,user) -> template.replace("_user_", user.getName());

String emailTemplate = "Hello _user_";

// (user) => "Hello " + user.getUserName()Function<User, String> contentForUser = emailTxt.apply(emailTemplate);

11/03/201744

Page 45: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Partial application

// (template, user) => ContentsFunction2<String, User, String> emailTxt =

(template,user) -> template.replace("_user_", user.getName());

String emailTemplate = "Hello _user_";

// (user) => "Hello " + user.getUserName()Function<User, String> contentForUser = emailTxt.apply(emailTemplate);

users.filter(x -> Objects.nonNull(x.getName()))

11/03/201745

Page 46: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Partial application

// (template, user) => ContentsFunction2<String, User, String> emailTxt =

(template,user) -> template.replace("_user_", user.getName());

String emailTemplate = "Hello _user_";

// (user) => "Hello " + user.getUserName()Function<User, String> contentForUser = emailTxt.apply(emailTemplate);

users.filter(x -> Objects.nonNull(x.getName())) .forEach(user -> emailService.sendEmail( user.getEmail(), contentForUser.apply(user)));

11/03/201746

Page 47: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Tuples

● Easily create tuples of length 1 to 8○ Tuple.of(1, “two”, Option.empty())

11/03/201747

Page 48: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Tuples

● Easily create tuples of length 1 to 8○ Tuple.of(1, “two”, Option.empty())

List<Status> statuses = users.map(user ->emailService.sendEmail(

user.getEmail(), contentForUser.apply(user.getName())));

11/03/201748

Page 49: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Tuples

● Easily create tuples of length 1 to 8○ Tuple.of(1, “two”, Option.empty())

List<Status> statuses = users.map(user ->emailService.sendEmail(

user.getEmail(), contentForUser.apply(user.getName())));

List<Tuple2<User, Status>> mailStatusForUser = users.zip(statuses);// Status = OK | NOT_OK

11/03/201749

Page 50: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Checked Functions● Lambdas in Java8 can’t throw checked exceptions

// Compiler errorSupplier<InputStream> inSupplier = socket::getInputStream;

11/03/201750

Page 51: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Checked Functions● Lambdas in Java8 can’t throw checked exceptions

// Compiler errorSupplier<InputStream> inSupplier = socket::getInputStream;

● Javaslang provides checked functionsCheckedFunction0<BufferedReader> readerSupplier = CheckedFunction0.of(socket::getInputStream) .andThen(InputStreamReader::new) .andThen(BufferedReader::new);

11/03/201751

Page 52: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Checked Functions● Lambdas in Java8 can’t throw checked exceptions

// Compiler errorSupplier<InputStream> inSupplier = socket::getInputStream;

● Javaslang provides checked functionsCheckedFunction0<BufferedReader> readerSupplier = CheckedFunction0.of(socket::getInputStream) .andThen(InputStreamReader::new) .andThen(BufferedReader::new);

try { readerSupplier.apply();} catch (Throwable throwable) { // do something}

11/03/201752

Page 53: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Error Handling● Checked functions can be composed in a clean way● But there is an even more elegant solution.

11/03/201753

Page 54: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Error Handling● Checked functions can be composed in a clean way● But there is an even more elegant solution. Instead of:

CheckedFunction0<InputStream> inCheckedSuplier =CheckedFunction0.of(socket::getInputStream);

try { inCheckedSuplier.apply();} catch (Throwable throwable) { // do something}

11/03/201754

Page 55: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Error Handling● Checked functions can be composed in a clean way● But there is an even more elegant solution. Instead of:

CheckedFunction0<InputStream> inCheckedSuplier =CheckedFunction0.of(socket::getInputStream);

try { inCheckedSuplier.apply();} catch (Throwable throwable) { // do something}

● We could just doTry<BufferedReader> readerTry = Try.of(socket::getInputStream) .map(InputStreamReader::new) .map(BufferedReader::new);

11/03/201755

Page 56: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

How we usually do it@RequestMapping("/person/{name}")public ResponseEntity<?> find(String name) { }

11/03/201756

Page 57: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

How we usually do it@RequestMapping("/person/{name}")public ResponseEntity<?> find(String name) { if (!validate(name)) { return ResponseEntity.badRequest().body("request not valid"); }}

11/03/201757

Page 58: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

How we usually do it@RequestMapping("/person/{name}")public ResponseEntity<?> find(String name) { if (!validate(name)) { return ResponseEntity.badRequest().body("request not valid"); }

Person somePerson = this.someService.find(name);}

11/03/201758

Page 59: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

How we usually do it@RequestMapping("/person/{name}")public ResponseEntity<?> find(String name) { if (!validate(name)) { return ResponseEntity.badRequest().body("request not valid"); }

Person somePerson = this.someService.find(name); return somePerson == null ? ResponseEntity.notFound().build()}

11/03/201759

Page 60: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

How we usually do it@RequestMapping("/person/{name}")public ResponseEntity<?> find(String name) { if (!validate(name)) { return ResponseEntity.badRequest().body("request not valid"); }

Person somePerson = this.someService.find(name); return somePerson == null ? ResponseEntity.notFound().build() : ResponseEntity.ok(somePerson);}

11/03/201760

Page 61: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

How we usually do it@RequestMapping("/person/{name}")public ResponseEntity<?> find(String name) { if (!validate(name)) { return ResponseEntity.badRequest().body("request not valid"); }

Person somePerson = this.someService.find(name); return somePerson == null ? ResponseEntity.notFound().build() : ResponseEntity.ok(somePerson);}

// What if someService.find throws some exception?// What if validate throws some exception?

11/03/201761

Page 62: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Let’s play with it

@RequestMapping("/find/{name}")public ResponseEntity<?> find(String name) { return validate(name) // returns Either<Throwable, String>}

11/03/201762

Page 63: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Let’s play with it

@RequestMapping("/find/{name}")public ResponseEntity<?> find(String name) { return validate(name) // returns Either<Throwable, String> .flatMap(this.someService::find) // only if validation passed}

// SomeService.find returns Either a person (correct result)// or a Throwable on an Errorpublic Either<Throwable, Person> find(String name)

11/03/201763

Page 64: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Let’s play with it

@RequestMapping("/find/{name}")public ResponseEntity<?> find(String name) { return validate(name) // returns Either<Throwable, String> .flatMap(this.someService::find) // only if validation passed .fold(this::getResponseOnError, ResponseEntity::ok);

//always returns a response}

11/03/201764

Page 65: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Some of us have to work with InputStreamsString getContent(String location) throws IOException { try { final URL url = new URL(location); if (!"http".equals(url.getProtocol())) { throw new UnsupportedOperationException("Protocol is not http"); }

final URLConnection con = url.openConnection(); final InputStream in = con.getInputStream(); return readAndClose(in); } catch(Exception x) { throw new IOException("Error loading location " + location, x); }}

11/03/201765

Page 66: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Let’s fix itTry<String> getContentT(String location) { return Try .of(() -> new URL(location)) }

11/03/201766

Page 67: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Let’s fix itTry<String> getContentT(String location) { return Try .of(() -> new URL(location)) .filter(url -> "http".equals(url.getProtocol())) }

11/03/201767

Page 68: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Let’s fix itTry<String> getContentT(String location) { return Try .of(() -> new URL(location)) .filter(url -> "http".equals(url.getProtocol())) .flatMap(url -> Try.of(url::openConnection))

}

11/03/201768

Page 69: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Let’s fix itTry<String> getContentT(String location) { return Try .of(() -> new URL(location)) .filter(url -> "http".equals(url.getProtocol())) .flatMap(url -> Try.of(url::openConnection)) .flatMap(con -> Try.of(con::getInputStream)) }

11/03/201769

Page 70: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Let’s fix itTry<String> getContentT(String location) { return Try .of(() -> new URL(location)) .filter(url -> "http".equals(url.getProtocol())) .flatMap(url -> Try.of(url::openConnection)) .flatMap(con -> Try.of(con::getInputStream)) .map(this::readAndClose);}

11/03/201770

Page 71: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Conclusion

● Right tool for the right job

11/03/201771

Page 72: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Conclusion

● Right tool for the right job● We’re still struggling with proper use of OOP

11/03/201772

Page 73: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Conclusion

● Right tool for the right job● We’re still struggling with proper use of OOP● Pure functional programming is hard

11/03/201773

Page 74: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Conclusion

● Right tool for the right job● We’re still struggling with proper use of OOP● Pure functional programming is hard● Javaslang offers good functional patterns● And we can combine them with our Java code

○ When we need them, if we need them

11/03/201774

Page 75: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Conclusion

● Right tool for the right job● We’re still struggling with proper use of OOP● Pure functional programming is hard● Javaslang offers good functional patterns● And we can combine them with our Java code

○ When we need them, if we need them● It offers a lot more than I talked about here

11/03/201775

Page 76: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Conclusion

● Right tool for the right job● We’re still struggling with proper use of OOP● Pure functional programming is hard● Javaslang offers us good functional patterns● And we can combine them with our Java code

○ When we need them, if we need them● It offers a lot more than I talked about here● So please check it out

○ http://www.javaslang.io/○ https://github.com/javaslang/javaslang

11/03/201776

Page 77: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

Thank you!Questions?

21/02/2017 77

Page 78: Javaslang Achieve functional eloquence in Javajug.mk/presentations/javaskop17/Blagoj.pdf · Why functional programming? What kind of input am I getting? Is it mutable? Will someone

11/03/2017 78

SORSIX International is an established Australian IT company with offices in Australia, USA, Macedonia and Serbia. We build mission-critical systems for finance, telecommunications and healthcare. Our systems keep planes flying, banks working, and phones connected. Ten million people live and prosper on our healthcare platform, spanning three countries. Sorsix believes in building systems that never go down because lives and businesses depend on them.

About Sorsix

All Sorsix trademarks and logos are owned by Sorsix. All other brand or product names are trademarks or registered marks of their respective owners. Because we are continually improving our products and services, Sorsix reserves the right to change specifications without prior notice.

©2016 Sorsix Ltd. All rights reserved.