Android @ RedMart
1
Adnan A M
droidcon Berlin 2019 2
Slide 3
Slide 3 text
Why is FP hard ?
droidcon Berlin 2019 3
Slide 4
Slide 4 text
It isn't ...
droidcon Berlin 2019 4
Slide 5
Slide 5 text
Perception vs Reality
droidcon Berlin 2019 5
Slide 6
Slide 6 text
Challenge isn't in learning,
it's in unlearning !
droidcon Berlin 2019 6
Slide 7
Slide 7 text
What does it mean to be functional ?
4 Declarative/Explicit
4 Immutability/Concurrency
4 Purity
4 Referential Transparency
4 State Isolation
droidcon Berlin 2019 7
Slide 8
Slide 8 text
Declarative vs Imperative
Imperative -> How ?
Declarative -> What ?
droidcon Berlin 2019 8
Slide 9
Slide 9 text
Declarative vs Imperative
4 I see an empty table at the end of the room. I and my
spouse are going to walk over there and sit down
4 Table for two please
droidcon Berlin 2019 9
Slide 10
Slide 10 text
Declarative vs Imperative
Imperative focuses on the How
val array = listOf(1,2,3,4)
var sum = 0
var odds: MutableList = listOf()
for (element in array) {
sum += element
if (element % 2 == 1) {
odds.add(element)
}
}
droidcon Berlin 2019 10
Slide 11
Slide 11 text
Declarative vs Imperative
Declarative focuses on the What
val array = listOf(1,2,3,4)
val sum = array.reduce { sum, i -> sum + i }
val odds = array.filter { i -> i % 2 == 1 }
droidcon Berlin 2019 11
Slide 12
Slide 12 text
Explicit
4 A function must always have a well defined return
type
4 Avoid void/Unit as return types
droidcon Berlin 2019 12
Slide 13
Slide 13 text
Explicit
fun multiplyBy5(number: Int): Int = { /** Some complex Math **/ }
fun asyncCall(callback: CustomListener) = { /** Some IO operation and return via callback **/ }
droidcon Berlin 2019 13
Slide 14
Slide 14 text
Immutablity/Concurrency
droidcon Berlin 2019 14
Slide 15
Slide 15 text
Immutability/Concurrency
droidcon Berlin 2019 15
Slide 16
Slide 16 text
Purity
droidcon Berlin 2019 16
Slide 17
Slide 17 text
Pure Functions
4 Produce same output for same inputs
4 Rely only on input parameters
4 Do not have any side effects
droidcon Berlin 2019 17
Slide 18
Slide 18 text
Side Effects
Modifying anything outside the scope or requiring
external context.
Examples :
4 Writing to a file/database
4 Making a network request
4 Modifying variables/state
droidcon Berlin 2019 18
Slide 19
Slide 19 text
Pure Functions - Sample
val z = 10;
fun add(x: Int, y: Int): Int {
return x + y;
}
droidcon Berlin 2019 19
Slide 20
Slide 20 text
Pure Functions - Sample
Most useful Pure Functions must take at least 1 parameter
fun justConstant(): Int {
return 1336;
}
droidcon Berlin 2019 20
Slide 21
Slide 21 text
Pure Functions - Sample
Most useful Pure Functions must return a value
fun noReturn(x:Int, y:Int) {
val z = x + y
}
droidcon Berlin 2019 21
Slide 22
Slide 22 text
Referential Transparency
droidcon Berlin 2019 22
Slide 23
Slide 23 text
Referential Transparency
fun referentiallyTransparent(x:Int, y:Int): Int {
val z = x + y
return z
}
droidcon Berlin 2019 23
Slide 24
Slide 24 text
Benefits of Pure Functions
4 Unit Testing
4 Paralellization
4 Ordering
4 Memoization
droidcon Berlin 2019 24
Slide 25
Slide 25 text
Parallelization
Run operations over multiple threads....
Ex: A parallel map implementation can distribute values over multiple
threads and collect the results
droidcon Berlin 2019 25
Slide 26
Slide 26 text
Ordering
fun doSomeWork(array: List): Work {
doWork2(array)
doWork1(array)
doWork3(array)
return Work(doWork1(array))
}
droidcon Berlin 2019 26
Slide 27
Slide 27 text
Memoization
Since O/P's don't change for same I/P's we can cache and
re-use their results
droidcon Berlin 2019 27
Slide 28
Slide 28 text
No Side Effects ?
droidcon Berlin 2019 28
Slide 29
Slide 29 text
No Side Effects ?
Functional Programming doesn't eliminate Side Effects,
but contains them and separates them out.
droidcon Berlin 2019 29
Slide 30
Slide 30 text
State Isolation
Move state modifications to outer boundaries
droidcon Berlin 2019 30
Slide 31
Slide 31 text
Function Composition
droidcon Berlin 2019 31
Slide 32
Slide 32 text
Exception Handling
droidcon Berlin 2019 32
Slide 33
Slide 33 text
Designing a User Profile
droidcon Berlin 2019 33
Slide 34
Slide 34 text
User Profile
public class User {
private String Name;
private String email;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
droidcon Berlin 2019 34
Slide 35
Slide 35 text
Stop Thinking
Classes, Start
Thinking Functions
droidcon Berlin 2019 35
Slide 36
Slide 36 text
FP is not opp of OOP !
Imperative shell on the outside with an FP core on the inside
droidcon Berlin 2019 36
Slide 37
Slide 37 text
Making it a habit
droidcon Berlin 2019 37
Slide 38
Slide 38 text
# 1
Can this be generic ?
droidcon Berlin 2019 38
Slide 39
Slide 39 text
# 2
Make All Things Immutable !
droidcon Berlin 2019 39
Slide 40
Slide 40 text
# 3
Stop using loops
droidcon Berlin 2019 40
Slide 41
Slide 41 text
# 4
Stop using if constructs
droidcon Berlin 2019 41
Slide 42
Slide 42 text
The 30 Day Challenge
No loops
No var (Use with caution)
No if
droidcon Berlin 2019 42
Slide 43
Slide 43 text
Caveats aka How To Not
Shoot Yourself In The Foot
droidcon Berlin 2019 43
Slide 44
Slide 44 text
# 1
It isn't an all or nothing game !
droidcon Berlin 2019 44
Slide 45
Slide 45 text
# 2
Trying to be over functional
droidcon Berlin 2019 45
Slide 46
Slide 46 text
# 3
Learn At Your Own Pace
droidcon Berlin 2019 46
Slide 47
Slide 47 text
# 4
Have fun
Don't stress it
droidcon Berlin 2019 47
Slide 48
Slide 48 text
# 5
Make Your Life Easier
droidcon Berlin 2019 48
Slide 49
Slide 49 text
Arrow-Kt
droidcon Berlin 2019 49
Slide 50
Slide 50 text
Arrow-Kt Samples
droidcon Berlin 2019 50
Slide 51
Slide 51 text
Either Type
// Old Exception Style
fun parse(s: String): Int =
if (s.matches(Regex("-?[0-9]+"))) s.toInt()
else throw NumberFormatException("$s is not a valid integer.")
// Either Style
fun parse(s: String): Either =
if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt())
else Either.Left(NumberFormatException("$s is not a valid integer."))
droidcon Berlin 2019 51
Slide 52
Slide 52 text
Either Type
val x = parse("2")
val value = when(x) {
is Either.Left -> when (x.a){
is NumberFormatException -> "Not a number!"
else -> "Unknown error"
}
is Either.Right -> "Got Number: ${x.b}"
}
droidcon Berlin 2019 52
Slide 53
Slide 53 text
Option Type
// Some value contained
val someValue: Option = Some("I am wrapped in something")
//Empty value
val emptyValue: Option = None
// Call Site
val someValue: Option = Some(20.0)
val value = when(someValue) {
is Some -> someValue.t
is None -> 0.0
}
val number: Option = Some(3)
val noNumber: Option = None
val mappedResult1 = number.map { it * 1.5 }
val mappedResult2 = noNumber.map { it * 1.5 }
droidcon Berlin 2019 53
Slide 54
Slide 54 text
IO Computation
fun getDataFromNetwork(): IO>> { /** .... **/}
IO computation which returns either an error or a list of
users
droidcon Berlin 2019 54
Slide 55
Slide 55 text
References/Credits
4 47 Degrees - Arrow-kt
4 Charles Scalfani - So You Want To Be A Functional
Programmer
droidcon Berlin 2019 55
Slide 56
Slide 56 text
May The Functions Be With You !
4 Reach out to me at @AdnanM0123
4 Talk about OOP/FP or droidcon India or anything
Android
droidcon Berlin 2019 56