Who's More Functional: Kotlin, Groovy, Scala, or Java?

Preview:

DESCRIPTION

 

Citation preview

Groovy, Kotlin, Scala or Java?

Andrey Breslav

Who's More Functional

1Wednesday, October 3, 12

About Me

• Project lead of Kotlin ➡ at JetBrains since 2010

• EG member of JSR-335➡ Project Lambda

2

Andrey Breslav

2Wednesday, October 3, 12

Prologue

3

3Wednesday, October 3, 12

Prologue– Dad (or Mom), is Java a

functional language?

3

3Wednesday, October 3, 12

Prologue– Dad (or Mom), is Java a

functional language?

– Don't you know your dad from your mom?!

3

3Wednesday, October 3, 12

What is FP like?

4Wednesday, October 3, 12

5

Alonzo Church

1936

𝛌

5Wednesday, October 3, 12

5

Alonzo Church

1936

𝛌1946

5Wednesday, October 3, 12

5

Alonzo Church

1936

𝛌1946

5Wednesday, October 3, 12

5

Alonzo Church

1936

𝛌1946 1956

5Wednesday, October 3, 12

5

Alonzo Church

1936

𝛌1946 1956

John Backus

5Wednesday, October 3, 12

5

Alonzo Church

1936

𝛌1946 19761956

John Backus

Can Programming be Liberated from the von Neumann Style?

5Wednesday, October 3, 12

6

6Wednesday, October 3, 12

– Dad, what's good about FP?

6

6Wednesday, October 3, 12

– Dad, what's good about FP?

– It makes you look smart

6

6Wednesday, October 3, 12

– Dad, what's good about FP?

– It makes you look smart

– Like wearing glasses?

6

6Wednesday, October 3, 12

Effects (Mutability)

7

John von Neumann

State

com

man

ds

𝛌

f(g(x), y)VS

Inputs

Outputs

7Wednesday, October 3, 12

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Leonardo Fibonacci

8Wednesday, October 3, 12

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Leonardo Fibonacci

8Wednesday, October 3, 12

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Leonardo Fibonacci

Are you functional?

Groovy

Scala

Java 8

Kotlin

Recursion

V

V

V

V

8Wednesday, October 3, 12

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Leonardo Fibonacci

8Wednesday, October 3, 12

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Leonardo Fibonacci

8Wednesday, October 3, 12

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Leonardo Fibonacci

State

com

man

ds

9Wednesday, October 3, 12

Effect-Free

10

10Wednesday, October 3, 12

Effect-Free

10

10Wednesday, October 3, 12

Effect-Free

– Dad, how do I

10

10Wednesday, October 3, 12

Effect-Free

– Dad, how do I

- print("Hello")?

10

10Wednesday, October 3, 12

Effect-Free

– Dad, how do I

- print("Hello")?

- write to a file?

10

10Wednesday, October 3, 12

Effect-Free

– Dad, how do I

- print("Hello")?

- write to a file?

- do both?

10

10Wednesday, October 3, 12

Effect-Free

– Dad, how do I

- print("Hello")?

- write to a file?

- do both?

10

Are you functional?

Groovy

Scala

Java 8

Kotlin

Recursion

V

V

V

V

X

X

Pure

X

X

10Wednesday, October 3, 12

Summary

• FP makes things simpler

• Sometimes at a huge price

• Our languages are not purely functional

11

11Wednesday, October 3, 12

Higher OrderFP brings order :)

12Wednesday, October 3, 12

FibonacciTest

13

13Wednesday, October 3, 12

FibonacciTest

13

– How do I test both implementations?

13Wednesday, October 3, 12

test(f)

14

14Wednesday, October 3, 12

test(f)

14

Are you functional?

Groovy

Scala

Java 8

Kotlin

Recursion

V

V

V

V

X

X

Pure

X

X

V

V

V

V

HO

14Wednesday, October 3, 12

GoF and FP

15

Strategy

Command Observer AbstractFactory

State Visitor

= Higher-Order

Function

15Wednesday, October 3, 12

Internal Iteration

16

16Wednesday, October 3, 12

Internal Iteration

16

users: ArrayList

user 1

user 2

user 3

user 4

Hello

Hello

Hello

Hello

16Wednesday, October 3, 12

Internal Iteration

16

users: ArrayList

user 1

user 2

user 3

user 4

Hello

Hello

Hello

Hello

users: ParallelCollection

16Wednesday, October 3, 12

Internal Iteration

16

users: ArrayList

user 1

user 2

user 3

user 4

Hello

Hello

Hello

Hello

users: ParallelCollection

16Wednesday, October 3, 12

Internal Iteration

16

users: ArrayList

user 1

user 2

user 3

user 4

Hello

Hello

Hello

Hello

users: ParallelCollection

16Wednesday, October 3, 12

Internal Iteration

16

users: ArrayList

user 1

user 2

user 3

user 4

Hello

Hello

Hello

Hello

users: ParallelCollection

16Wednesday, October 3, 12

Internal Iteration

16

users: ArrayList

user 1

user 2

user 3

user 4

Hello

Hello

Hello

Hello

users: ParallelCollection

16Wednesday, October 3, 12

Summary

• Good old callbacks/strategies

• Very important abstraction

17

17Wednesday, October 3, 12

ADTWhat is your "A" for?

18Wednesday, October 3, 12

Example: Messages

19

Server Client

19Wednesday, October 3, 12

Example: Messages

19

Server Clientsearch for "lambda"

19Wednesday, October 3, 12

Example: Messages

19

Server Clientsearch for "lambda"

exact match at ...

19Wednesday, October 3, 12

Example: Messages

19

Server Clientsearch for "lambda"

exact match at ...

similarity at ...

19Wednesday, October 3, 12

Example: Messages

19

message ::= search for term | exact item | similar item

Server Clientsearch for "lambda"

exact match at ...

similarity at ...

19Wednesday, October 3, 12

20

ADT

Algebraicdata type

Abstractdata type

~ Interface

~ Data class

20Wednesday, October 3, 12

20

ADT

Algebraicdata type

Abstractdata type

~ Interface

~ Data class

Are you functional?

Groovy

Scala

Java 8

Kotlin

Recursion

V

V

V

V

X

X

Pure

X

X

V

V

V

V

HO

V

V

V

V

ADT

20Wednesday, October 3, 12

ADT in Kotlin

21

21Wednesday, October 3, 12

UtilizationYou may have your cake, but can you eat it too?

22Wednesday, October 3, 12

Up to 5 HO-functions for free!

Upgrade

for

more

23Wednesday, October 3, 12

Static Utility Methods

24

backwards!

24Wednesday, October 3, 12

Static Utility Methods

24

backwards!

as opposed to

24Wednesday, October 3, 12

Extension Functions

25

25Wednesday, October 3, 12

Extension Functions

25

Receiver Type

25Wednesday, October 3, 12

Extension Functions

25

Receiver Type

receiver

25Wednesday, October 3, 12

Summary

• Extending existing types

• Without changing the classes

26

26Wednesday, October 3, 12

Epilogue

27Wednesday, October 3, 12

Epilogue– Dad, should I use FP?

27Wednesday, October 3, 12

Epilogue– Dad, should I use FP?

– What is your problem?!

27Wednesday, October 3, 12

Epilogue– Dad, should I use FP?

– What is your problem?!

P.S. Kotlin is cool :) http://kotlin.jetbrains.org27Wednesday, October 3, 12

Kotlin Resources

• Docs: http://kotlin.jetbrains.org

• Demo: http://kotlin-demo.jetbrains.com

• Code: http://github.com/jetbrains/kotlin

• Twitter:➡ @project_kotlin

➡ @abreslav

28

28Wednesday, October 3, 12

Recommended