Top Banner
Reaching the lambda heaven ıctor Orozco February 23, 2017 Nabenik 1
54

Reaching the lambda heaven

Mar 20, 2017

Download

Internet

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: Reaching the lambda heaven

Reaching the lambda heaven

V́ıctor Orozco

February 23, 2017

Nabenik

1

Page 2: Reaching the lambda heaven

About

2

Page 3: Reaching the lambda heaven

Intro

Student 1 - How do I

jump the CS theory to

do ”real development”

with Java 8?3

Page 4: Reaching the lambda heaven

Intro

Street developer 1 - How

do I jump the CS theory

to do ”real development”

with Java 8?4

Page 5: Reaching the lambda heaven

5

Page 6: Reaching the lambda heaven

Intro

How do I learn FP to do

”street development”

with Java 8?

5

Page 7: Reaching the lambda heaven

Intro

What should I learn FP

to do ”street

development” with Java

8?6

Page 8: Reaching the lambda heaven

Outline

Java 8

FP

Functional blocks

Functional JDK

Libraries

QA

7

Page 9: Reaching the lambda heaven

Java 8

Page 10: Reaching the lambda heaven

Java 8

Release date: 2014-03-18 - 3 years ago!!

https://www.oracle.com/java8

https://www.oracle.com/java8launch

8

Page 11: Reaching the lambda heaven

Java 8

• Nashorn

• Date/Time API

• Compact Profiles

• Type Annotations

• Default methods

• Streams

• Lambda

Expressions

9

Page 12: Reaching the lambda heaven

Paradigms (Simplification)

Paradigms

Imperative

Structured

Pascal

OOP

Java

Declarative

Functional

Clojure

Logic

Prolog

10

Page 13: Reaching the lambda heaven

1-2-3-4 of FP

1. Computation = Function evaluation

2. NO state changes

3. NO mutability

4. Declarative → Expressions

11

Page 14: Reaching the lambda heaven

Java vs. Functional (organization - think

about)

Java Classes

FP Functions

12

Page 15: Reaching the lambda heaven

Java vs. Functional (algorithms - write code

as)

Java Imperative, behavior as steps

FPDeclarative, interac-

tion between functions

13

Page 16: Reaching the lambda heaven

Java vs. Functional (Mutability and state

- manipulate or not)

JavaState and behavior together,

mutability is intrinsic

FP Avoids state

14

Page 17: Reaching the lambda heaven

Java vs. Functional (Style - Good looking

code)

Java OOP + Design patterns

FP Abstraction by itself

15

Page 18: Reaching the lambda heaven

Java vs. Functional (Concurrency - The

most hated thing in the development world)

Java Locks and shared resources

FP Workflows (parallel)

16

Page 19: Reaching the lambda heaven

Java vs. Functional (Code - Result)

Java Descriptive (Too-much?)

FP Dense

17

Page 20: Reaching the lambda heaven

Java 8

An OOP language with functional additions

18

Page 21: Reaching the lambda heaven

FP

Page 22: Reaching the lambda heaven

Why?

1. Easy parallelism

2. Elegance

3. Good with reactive

19

Page 23: Reaching the lambda heaven

FP on Java

• Java is not purely functional

• Other options (Scala, Kotlin, Ceylon)

• Java supports FP through libraries

20

Page 24: Reaching the lambda heaven

FP on Java (Consequence)

21

Page 25: Reaching the lambda heaven

FP on Java (Consequence)

Java 8 spawned a new

ecosystem of functional

(and declarative . . . and

reactive) libraries

22

Page 26: Reaching the lambda heaven

The heaven

Q - How do I reach it?

23

Page 27: Reaching the lambda heaven

The heaven

A - Using a starway

24

Page 28: Reaching the lambda heaven

1 - Blocks

25

Page 29: Reaching the lambda heaven

1 - Functional blocks in Java 8

• Lambda expressions

• Functional interface

• High order functions

• Complements (predicates, method reference)

26

Page 30: Reaching the lambda heaven

2 - The JDK

27

Page 31: Reaching the lambda heaven

2 - Functional JDK

• Pre-defined functions in Java 8

(java.util.function)

• More than 40 functional interfaces

• Streams API

28

Page 32: Reaching the lambda heaven

3 - Libraries

29

Page 33: Reaching the lambda heaven

3 - Libraries

Cathedral

• Java EE (Reactive on the road)

• Spring (FP Ready)

• Lagom (Reactive oriented)

Bazaar

• JavaSlang, jOOQ/jOOL, EclipseCollections,

FunctionalJava (Functionality)

• RxJava, Akka, Vert.x (Interactions -

Architecture)

30

Page 34: Reaching the lambda heaven

Functional blocks

Page 35: Reaching the lambda heaven

Lambda expression

Anonymous function (behavior)

//In-line

(String x) -> System.out.println(x);

//Multi -line

(x) -> {

System.out.println(x);

}

//From static

System.out:: println;

31

Page 36: Reaching the lambda heaven

Functional interfaces

• Just one abstract method

• Interfaces allow default methods

@FunctionalInterface

public interface MyFunctionalInterface

{

String doFunctional(String a, String b);

}

32

Page 37: Reaching the lambda heaven

High order functions

• Functions as arguments and return values

MyFunctionalInterface doHoFunction

(MyfunctionalInterface param ){

String result = param.doFunctional(

"Marco", "Polo");

return (x,y) -> x.concat(y);

}

33

Page 38: Reaching the lambda heaven

Functional JDK

Page 39: Reaching the lambda heaven

Streams API

• Map-Reduce

• ”Monad” like

Stream Op 1 Op 2 Terminal

34

Page 40: Reaching the lambda heaven

Streams API

Declarative - Initial

List <String > goodJvmLangs =

Arrays.asList("Java", "Kotlin", "Ceylon");

// Method reference

goodJvmLangs.forEach(System.out:: println );

35

Page 41: Reaching the lambda heaven

Streams API - Predicates

List <String > goodJvmLangs =

Arrays.asList("Java", "Kotlin", "C#");

Predicate <String > badRemover =

s -> "C#".equals(s);

goodJvmLangs.removeIf(badRemover );

36

Page 42: Reaching the lambda heaven

Streams API - Map-reduce

winners

.stream () // Steam

.filter( // Intermediate

p -> p.getOrderId ()

.equals(winnerId)

)

.findFirst (); // Reduce

37

Page 43: Reaching the lambda heaven

Streams API - Map-reduce

unfilteredList.stream ()

.map(x -> x-1) //Real ap

.filter(x -> x > 50) // Other intermediate app

.collect(Collectors.toList ()); // Reduce

38

Page 44: Reaching the lambda heaven

Libraries

Page 45: Reaching the lambda heaven

JavaSlang

Offer

• Java core library

• Immutable

collections

• Control structures

Good for

• Elegant code

• Eliminating the

.stream() and

.collect() in streams

• Exception handling

(Try monad)

39

Page 46: Reaching the lambda heaven

jOOQ

Offer

• Database first

• Typesafe SQL

• Code generation

Good for

• Natural SQL queries

• Low overhead queries

• Stream processing of

DB results

40

Page 47: Reaching the lambda heaven

Vert.x

Offer

• Reactive tool-kit

• Modular

• Scalable

Good for

• Reactive backend

and/or microservices

• Compatible with

RxJava

• Alternative

framework

41

Page 48: Reaching the lambda heaven

Complete sample

http://github.com/tuxtor/fpjavademo2

42

Page 49: Reaching the lambda heaven

Bazaar caveats

• Many library features overlap (Cyclops -

https://github.com/aol/cyclops)

• Streams API improvements in Java 9

https://www.voxxed.com/blog/2017/02/

java-9-streams-api/

• Huge POM.xml :)

43

Page 50: Reaching the lambda heaven

FP - The good parts

• Fun

• Declarative

• Less and elegant code

44

Page 51: Reaching the lambda heaven

FP - The bad parts

• Performance - (maybe)

• Debug

• Learning curve

45

Page 52: Reaching the lambda heaven

Books and resources

• JDK 8 Lamdas&Streams MOOC https://www.

youtube.com/playlist?list=PLMod1hYiIvSZL1xclvHcsV2dMiminf19x

• Functional Programming in Java: Harnessingthe Power Of Java 8 Lambda Expressionshttp://www.amazon.com/

Functional-Programming-Java-Harnessing-Expressions/dp/1937785467

46

Page 53: Reaching the lambda heaven

QA

Page 54: Reaching the lambda heaven

Thank you

• @tuxtor

[email protected]

• http://vorozco.com

• http://github.com/tuxtor/slides

This work is licensed under a Creative Commons

Attribution-ShareAlike 3.0 License.

47