@Dorvaryn
+BenjaminAugustin-Dorvaryn
Dorvaryn
Benjamin Augustin
Android Software Craftsman
Slide 3
Slide 3 text
What is Functional
Programming ?
Slide 4
Slide 4 text
“In computer science, functional programming is
a programming paradigm that treats computation
as the evaluation of mathematical functions and
avoids changing-state and mutable data.”
- Wikipedia contributors, 2015
Slide 5
Slide 5 text
No content
Slide 6
Slide 6 text
● Less code
● Expressive code
● Small units
What’s the point ?
Slide 7
Slide 7 text
Focus on what not how
Slide 8
Slide 8 text
Core concepts
Slide 9
Slide 9 text
● Not mutable, unchangeable, changeless
● Modification -> new value
● Safe to cache
Immutability
Slide 10
Slide 10 text
● Same input -> Same output
● No side effect
● Referential transparency
Pure functions
Slide 11
Slide 11 text
No content
Slide 12
Slide 12 text
● Not a requirement but helps … a lot
● Building your domain
● Dealing with absence of data
Strongly typed
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
“I call it my billion-dollar mistake. It was
the invention of the null reference in 1965.”
- Sir Tony Hoare, 2009
● RxJava
● AutoValue
● Guava
● Or… Kotlin ?
For java 7 and lower
Slide 19
Slide 19 text
No content
Slide 20
Slide 20 text
Kotlin Syntax
fun aFunction(parameter: String): String {
return parameter
}
fun highOrder(param: Int, func: (Int) -> Int): Int {
return func(param)
}
val lambda = { x: Int, y: Int -> x + y }
Slide 21
Slide 21 text
Let’s get Functional
Slide 22
Slide 22 text
fun foo(words: Array): Map {
val result = mapOf()
for (word in words) {
val value = result.get(word)
if (value != null) {
result.set(word, value + 1)
} else {
result.set(word, 1)
}
}
return result
}
What does foo do ?
Slide 23
Slide 23 text
No content
Slide 24
Slide 24 text
fun foo(words: Array): List> {
return words.groupBy {
word -> word
}.map {
Pair(it.key, it.value.count())
}
}
Can you tell now ?
Slide 25
Slide 25 text
● Code should be self explanatory
● Not just the method name
● Keep things concise
Expressive code
Slide 26
Slide 26 text
● Get items occurring at least 3 times
● Sort by occurrences
● As a list of strings
Extensible code
● Article, Product, User...
● Value objects -> Immutable
● Lists of value objects
What is your data ?
Slide 32
Slide 32 text
● Fetching a list of users
● Calculating the price of a basket
● Input List -> Output List
What is your processing ?
Slide 33
Slide 33 text
Think like shell
#!/bin/bash
for jpg; do
png="${jpg%.jpg}.png"
if convert "$jpg" jpg.to.png ;
then
mv jpg.to.png "$png"
else
echo 'error' >&2
exit 1
fi
done
echo all conversions successful
exit 0
du -h | grep M | sort -nr | head -15
Slide 34
Slide 34 text
● map, flatMap, filter, findAll, merge, zip…..
● first, last, find, fold..…
● Decompose your processing using those functions
Functions upon lists
Slide 35
Slide 35 text
● Display the saved articles in a list
● Fetch the items in a basket requiring age check
● Any filtering logic -> list + filter
Don’t trust me ?
Slide 36
Slide 36 text
● Get the total of a basket
● Find an item in a list
● Any list traversal logic -> list + fold
Another one ?
Slide 37
Slide 37 text
No content
Slide 38
Slide 38 text
● f(x,y) applied on each element in a list
● x element in list
● y accumulator
Fold ?
Slide 39
Slide 39 text
Fold ?
Slide 40
Slide 40 text
Maximum using fold
+
+
+
->
->
->
Slide 41
Slide 41 text
No content
Slide 42
Slide 42 text
Basket example
Slide 43
Slide 43 text
data class Basket(val items: List)
data class Item(val barcode: String, val price: Double)
Slide 44
Slide 44 text
data class Basket(val items: List)
data class Item(val barcode: String, val price: Double)
fun calculateBasket(basket: Basket): Double {
var total = 0.0
for (item in basket.items) {
total = total + item.price
}
return total
}
Slide 45
Slide 45 text
data class Basket(val items: List)
data class Item(val barcode: String, val price: Double)
fun calculateBasket(basket: Basket): Double {
return basket.items
.fold(0.0, { total, item -> total + item.price })
}
Slide 46
Slide 46 text
data class Basket(val items: List)
data class Item(val barcode: String, val price: Double)
fun calculateBasket(basket: Basket): Double {
return basket.items
.fold(0.0, sum())
}
private fun sum() = {
total:Double, item: Item -> total + item.price
}
Slide 47
Slide 47 text
data class Basket(val items: List)
data class Item(val barcode: String, val price: Double)
fun calculateBasket(basket: Basket): Double {
var total = 0.0
for (item in basket.items) {
total = sum(item, total)
}
return total
}
private fun sum(item: Item, total: Double) = {
total + item.price
}
Slide 48
Slide 48 text
data class Basket(val items: List)
data class Item(val barcode: String, val price: Double)
fun calculateBasket(basket: Basket): Double {
return basket.items
.fold(0.0, sum())
}
private fun sum() = {
total:Double, item: Item -> total + item.price
}
Slide 49
Slide 49 text
No content
Slide 50
Slide 50 text
No content
Slide 51
Slide 51 text
No content
Slide 52
Slide 52 text
Make your data immutable
Start small
Aim for pure functions
Validate your data
Focus on what not how
Slide 53
Slide 53 text
Learn a functional language
Keep learning
Spend some time on pet projects
Have fun !
Slide 54
Slide 54 text
No content
Slide 55
Slide 55 text
No content
Slide 56
Slide 56 text
● Learn you a Haskell for great good by Miran Lipovača
● Functional Programming in Java by Venkat Subramaniam
● Category Theory for Programmers by Bartosz Milewski
Good reads
Slide 57
Slide 57 text
Questions ?
Slide 58
Slide 58 text
@Dorvaryn
+BenjaminAugustin-Dorvaryn
Dorvaryn
Benjamin Augustin
Android Software Craftsman