Who's More Functional: Kotlin, Groovy, Scala, or Java?
-
Upload
andrey-breslav -
Category
Technology
-
view
4.091 -
download
0
description
Transcript of Who's More Functional: Kotlin, Groovy, Scala, or Java?
![Page 1: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/1.jpg)
Groovy, Kotlin, Scala or Java?
Andrey Breslav
Who's More Functional
1Wednesday, October 3, 12
![Page 2: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/2.jpg)
About Me
• Project lead of Kotlin ➡ at JetBrains since 2010
• EG member of JSR-335➡ Project Lambda
2
Andrey Breslav
2Wednesday, October 3, 12
![Page 3: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/3.jpg)
Prologue
3
3Wednesday, October 3, 12
![Page 4: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/4.jpg)
Prologue– Dad (or Mom), is Java a
functional language?
3
3Wednesday, October 3, 12
![Page 5: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/5.jpg)
Prologue– Dad (or Mom), is Java a
functional language?
– Don't you know your dad from your mom?!
3
3Wednesday, October 3, 12
![Page 6: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/6.jpg)
What is FP like?
4Wednesday, October 3, 12
![Page 7: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/7.jpg)
5
Alonzo Church
1936
𝛌
5Wednesday, October 3, 12
![Page 8: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/8.jpg)
5
Alonzo Church
1936
𝛌1946
5Wednesday, October 3, 12
![Page 9: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/9.jpg)
5
Alonzo Church
1936
𝛌1946
5Wednesday, October 3, 12
![Page 10: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/10.jpg)
5
Alonzo Church
1936
𝛌1946 1956
5Wednesday, October 3, 12
![Page 11: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/11.jpg)
5
Alonzo Church
1936
𝛌1946 1956
John Backus
5Wednesday, October 3, 12
![Page 12: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/12.jpg)
5
Alonzo Church
1936
𝛌1946 19761956
John Backus
Can Programming be Liberated from the von Neumann Style?
5Wednesday, October 3, 12
![Page 13: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/13.jpg)
6
6Wednesday, October 3, 12
![Page 14: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/14.jpg)
– Dad, what's good about FP?
6
6Wednesday, October 3, 12
![Page 15: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/15.jpg)
– Dad, what's good about FP?
– It makes you look smart
6
6Wednesday, October 3, 12
![Page 16: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/16.jpg)
– Dad, what's good about FP?
– It makes you look smart
– Like wearing glasses?
6
6Wednesday, October 3, 12
![Page 17: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/17.jpg)
Effects (Mutability)
7
John von Neumann
State
com
man
ds
𝛌
f(g(x), y)VS
Inputs
Outputs
7Wednesday, October 3, 12
![Page 18: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/18.jpg)
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Leonardo Fibonacci
8Wednesday, October 3, 12
![Page 19: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/19.jpg)
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Leonardo Fibonacci
8Wednesday, October 3, 12
![Page 20: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/20.jpg)
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
![Page 21: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/21.jpg)
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Leonardo Fibonacci
8Wednesday, October 3, 12
![Page 22: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/22.jpg)
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Leonardo Fibonacci
8Wednesday, October 3, 12
![Page 23: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/23.jpg)
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Leonardo Fibonacci
State
com
man
ds
9Wednesday, October 3, 12
![Page 24: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/24.jpg)
Effect-Free
10
10Wednesday, October 3, 12
![Page 25: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/25.jpg)
Effect-Free
10
10Wednesday, October 3, 12
![Page 26: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/26.jpg)
Effect-Free
– Dad, how do I
10
10Wednesday, October 3, 12
![Page 27: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/27.jpg)
Effect-Free
– Dad, how do I
- print("Hello")?
10
10Wednesday, October 3, 12
![Page 28: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/28.jpg)
Effect-Free
– Dad, how do I
- print("Hello")?
- write to a file?
10
10Wednesday, October 3, 12
![Page 29: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/29.jpg)
Effect-Free
– Dad, how do I
- print("Hello")?
- write to a file?
- do both?
10
10Wednesday, October 3, 12
![Page 30: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/30.jpg)
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
![Page 31: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/31.jpg)
Summary
• FP makes things simpler
• Sometimes at a huge price
• Our languages are not purely functional
11
11Wednesday, October 3, 12
![Page 32: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/32.jpg)
Higher OrderFP brings order :)
12Wednesday, October 3, 12
![Page 33: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/33.jpg)
FibonacciTest
13
13Wednesday, October 3, 12
![Page 34: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/34.jpg)
FibonacciTest
13
– How do I test both implementations?
13Wednesday, October 3, 12
![Page 35: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/35.jpg)
test(f)
14
14Wednesday, October 3, 12
![Page 36: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/36.jpg)
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
![Page 37: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/37.jpg)
GoF and FP
15
Strategy
Command Observer AbstractFactory
State Visitor
= Higher-Order
Function
15Wednesday, October 3, 12
![Page 38: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/38.jpg)
Internal Iteration
16
16Wednesday, October 3, 12
![Page 39: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/39.jpg)
Internal Iteration
16
users: ArrayList
user 1
user 2
user 3
user 4
Hello
Hello
Hello
Hello
16Wednesday, October 3, 12
![Page 40: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/40.jpg)
Internal Iteration
16
users: ArrayList
user 1
user 2
user 3
user 4
Hello
Hello
Hello
Hello
users: ParallelCollection
16Wednesday, October 3, 12
![Page 41: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/41.jpg)
Internal Iteration
16
users: ArrayList
user 1
user 2
user 3
user 4
Hello
Hello
Hello
Hello
users: ParallelCollection
16Wednesday, October 3, 12
![Page 42: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/42.jpg)
Internal Iteration
16
users: ArrayList
user 1
user 2
user 3
user 4
Hello
Hello
Hello
Hello
users: ParallelCollection
16Wednesday, October 3, 12
![Page 43: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/43.jpg)
Internal Iteration
16
users: ArrayList
user 1
user 2
user 3
user 4
Hello
Hello
Hello
Hello
users: ParallelCollection
16Wednesday, October 3, 12
![Page 44: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/44.jpg)
Internal Iteration
16
users: ArrayList
user 1
user 2
user 3
user 4
Hello
Hello
Hello
Hello
users: ParallelCollection
16Wednesday, October 3, 12
![Page 45: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/45.jpg)
Summary
• Good old callbacks/strategies
• Very important abstraction
17
17Wednesday, October 3, 12
![Page 46: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/46.jpg)
ADTWhat is your "A" for?
18Wednesday, October 3, 12
![Page 47: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/47.jpg)
Example: Messages
19
Server Client
19Wednesday, October 3, 12
![Page 48: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/48.jpg)
Example: Messages
19
Server Clientsearch for "lambda"
19Wednesday, October 3, 12
![Page 49: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/49.jpg)
Example: Messages
19
Server Clientsearch for "lambda"
exact match at ...
19Wednesday, October 3, 12
![Page 50: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/50.jpg)
Example: Messages
19
Server Clientsearch for "lambda"
exact match at ...
similarity at ...
19Wednesday, October 3, 12
![Page 51: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/51.jpg)
Example: Messages
19
message ::= search for term | exact item | similar item
Server Clientsearch for "lambda"
exact match at ...
similarity at ...
19Wednesday, October 3, 12
![Page 52: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/52.jpg)
20
ADT
Algebraicdata type
Abstractdata type
~ Interface
~ Data class
20Wednesday, October 3, 12
![Page 53: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/53.jpg)
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
![Page 54: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/54.jpg)
ADT in Kotlin
21
21Wednesday, October 3, 12
![Page 55: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/55.jpg)
UtilizationYou may have your cake, but can you eat it too?
22Wednesday, October 3, 12
![Page 56: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/56.jpg)
Up to 5 HO-functions for free!
Upgrade
for
more
23Wednesday, October 3, 12
![Page 57: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/57.jpg)
Static Utility Methods
24
backwards!
24Wednesday, October 3, 12
![Page 58: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/58.jpg)
Static Utility Methods
24
backwards!
as opposed to
24Wednesday, October 3, 12
![Page 59: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/59.jpg)
Extension Functions
25
25Wednesday, October 3, 12
![Page 60: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/60.jpg)
Extension Functions
25
Receiver Type
25Wednesday, October 3, 12
![Page 61: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/61.jpg)
Extension Functions
25
Receiver Type
receiver
25Wednesday, October 3, 12
![Page 62: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/62.jpg)
Summary
• Extending existing types
• Without changing the classes
26
26Wednesday, October 3, 12
![Page 63: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/63.jpg)
Epilogue
27Wednesday, October 3, 12
![Page 64: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/64.jpg)
Epilogue– Dad, should I use FP?
27Wednesday, October 3, 12
![Page 65: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/65.jpg)
Epilogue– Dad, should I use FP?
– What is your problem?!
27Wednesday, October 3, 12
![Page 66: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/66.jpg)
Epilogue– Dad, should I use FP?
– What is your problem?!
P.S. Kotlin is cool :) http://kotlin.jetbrains.org27Wednesday, October 3, 12
![Page 67: Who's More Functional: Kotlin, Groovy, Scala, or Java?](https://reader033.fdocuments.net/reader033/viewer/2022051110/54b6d2f24a79594d158b45cb/html5/thumbnails/67.jpg)
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