Slide 1

Slide 1 text

Funktional Android Functional Programming for Android with Kotlin

Slide 2

Slide 2 text

What is Functional Programming?

Slide 3

Slide 3 text

What is Functional Programming? Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions.

Slide 4

Slide 4 text

Keys of FP

Slide 5

Slide 5 text

Keys of FP -First class and Higher-Order functions

Slide 6

Slide 6 text

Keys of FP -First class and Higher-Order functions -Referential transparency

Slide 7

Slide 7 text

Keys of FP -First class and Higher-Order functions -Referential transparency -Pure functions

Slide 8

Slide 8 text

Keys of FP -First class and Higher-Order functions -Referential transparency -Pure functions -Strict vs Non-Strict evaluation

Slide 9

Slide 9 text

Keys of FP Eliminating side effects can make it much easier to understand and predict the behavior of a program.

Slide 10

Slide 10 text

Monads

Slide 11

Slide 11 text

Monads A monad is just a monoid in the category of endofunctors, what's the problem?

Slide 12

Slide 12 text

Monads A monad is just a monoid in the category of endofunctors, what's the problem?

Slide 13

Slide 13 text

Monads

Slide 14

Slide 14 text

Monads Monads are wrappers with context for a value. So, like contexts, you can have different Monads for different use cases.

Slide 15

Slide 15 text

Monads Absence

Slide 16

Slide 16 text

Monads Absence Option // None or Some

Slide 17

Slide 17 text

Monads Absence Option // None or Some Runtime Exceptions

Slide 18

Slide 18 text

Monads Absence Option // None or Some Runtime Exceptions Try // Failure or Success

Slide 19

Slide 19 text

Monads Absence Option // None or Some Runtime Exceptions Try // Failure or Success Exceptional cases

Slide 20

Slide 20 text

Monads Absence Option // None or Some Runtime Exceptions Try // Failure or Success Exceptional cases Either // Left or Right

Slide 21

Slide 21 text

Monads Option(2).map { it + 3 }

Slide 22

Slide 22 text

Monads Option(2).map { it + 3 }

Slide 23

Slide 23 text

Handling exceptions with Rx useCase.fetchUser() .onErrorReturn(error -> null) .subscribe(user -> { if (user != null) … // Success case else … // Failure case })

Slide 24

Slide 24 text

Handling exceptions with Rx val userSingle = useCase.fetchUser() .onErrorReturn(error -> null)

Slide 25

Slide 25 text

Handling exceptions with Rx val userSingle = useCase.fetchUser() .onErrorReturn(error -> null) val companySingle = useCase.fetchUserCompany() .onErrorReturn(error -> null)

Slide 26

Slide 26 text

Handling exceptions with Rx val userSingle = useCase.fetchUser() .onErrorReturn(error -> null) val companySingle = useCase.fetchUserCompany() .onErrorReturn(error -> null) userSingle.zip(companySingle, (user, company) -> { if (user != null && company != null) … // Merge them else … // ??? }) .subscribe(userWithCompany -> // Success case)***Kind of

Slide 27

Slide 27 text

Handling exceptions with Monads

Slide 28

Slide 28 text

Handling exceptions with Monads // Forget about threading for a second :) val userResult: Either = … val companyResult: Either = … val userWithCompany = userResult.flatMap { user -> companyResult.map { company -> // Merge them } }

Slide 29

Slide 29 text

Handling exceptions with Monads // Forget about threading for a second :) val userResult: Either = … val companyResult: Either = … val userWithCompany = EitherMonad().binding { val user = bind { userResult } val company = bind { companyResult } yields(/* Merge them */) }

Slide 30

Slide 30 text

Handling exceptions with Monads // userWithCompany is an Either when(userWithCompany) { is Either.Left -> // Error case is Either.Right -> // Success case }

Slide 31

Slide 31 text

Monads vs Exceptions Pros: ● Safer code ● Less tests ● Less unexpected behaviours Cons: ● Complex code ● Advanced types + inference = higher compile times

Slide 32

Slide 32 text

But there's one more thing...

Slide 33

Slide 33 text

Monads

Slide 34

Slide 34 text

Handling exceptions with Monads val userResult: Either = … val companyResult: Either = … val userWithCompany = EitherMonad().binding { val user = bind { userResult } val company = bind { companyResult } yields(/* Merge them */) }

Slide 35

Slide 35 text

Handling exceptions with Monads val userResult: Future> = … val companyResult: Future> = … val userWithCompany = FutureMonad().binding { val user = bind { userResult } val company = bind { companyResult } yields(/* Merge them */) }

Slide 36

Slide 36 text

Handling exceptions with Monads val userResult: Future = … val companyResult: Future = … val userWithCompany = FutureMonad().binding { val user = bind { userResult } val company = bind { companyResult } yields(/* Merge them */) }

Slide 37

Slide 37 text

Handling exceptions with Monads val userResult: Future = … val companyResult: Future = … val userWithCompany = FutureMonad().binding { val user = bind { userResult } // Runs in parallel* val company = bind { companyResult } // Runs in parallel* yields(/* Merge them */) } *Potentially

Slide 38

Slide 38 text

Handling exceptions with Monads val userResult: Future = … val companyResult: Future = … val userWithCompany = FutureMonad().binding { val user = bind { userResult } val company = bind { companyResult } yields(/* Merge them */) } // WIP :)

Slide 39

Slide 39 text

Thanks

Slide 40

Slide 40 text

Questions?