Slide 1

Slide 1 text

Welcome

Slide 2

Slide 2 text

Introduction to Functional Programing in Kotlin and Arrow Hadi Tok Google Developers Expert (Android) Technical Lead, @CitizenMe https://haditok.com/

Slide 3

Slide 3 text

Functional Programming

Slide 4

Slide 4 text

Functional Programming In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.* *https://mauriziostorani.wordpress.com/2008/08/29/functional-programming-examples-methods-and-concepts/

Slide 5

Slide 5 text

Functional Programming In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.

Slide 6

Slide 6 text

Functional Programming In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.

Slide 7

Slide 7 text

Mathematical Functions A function is a relation for which each value from the set the first components of the ordered pairs is associated with exactly one value from the set of second components of the ordered pair.

Slide 8

Slide 8 text

Function { 1, 2, 3, 4} {A, B, C}

Slide 9

Slide 9 text

Function { 1, 2, 3, 4} {A, B, C}

Slide 10

Slide 10 text

Function { 1, 2, 3, 4} {A, B, C}

Slide 11

Slide 11 text

Functional Programming In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.

Slide 12

Slide 12 text

State class MyClass{ var list = listOf(1,2,3,4,5) fun filterList() { list = list.filter { it%2 !=0 } } }

Slide 13

Slide 13 text

State class MyClass{ val list = listOf(1,2,3,4,5) fun filterList(list:List): List { return list.filter { it!=0 } } }

Slide 14

Slide 14 text

Functional Programming In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.

Slide 15

Slide 15 text

Mutable data fun filterList(list:List){ val iterator=list.iterator() for(i in iterator){ if(i%2==0){ iterator.remove() } } }

Slide 16

Slide 16 text

Mutable data val filteredList = list.filter { it % 2 != 0 }

Slide 17

Slide 17 text

Changes in state and data ● Who is the owner of the data? ● What would happen when data changes? ● Should other constructs using the data needs to be notified? ● How other constructs will be notified? ● What happens when there are multiple changes at the same time?

Slide 18

Slide 18 text

Pure functions ● Its return value is the same for the same arguments ● Has no side effects

Slide 19

Slide 19 text

Side effects ● Mutation of local static variables,non-local variables, mutable reference arguments ● Throwing an exception ● I/O operations

Slide 20

Slide 20 text

Kotlin and Functional Programming

Slide 21

Slide 21 text

Package Level Functions class Logger{ fun log(message: String){ println(message) } }

Slide 22

Slide 22 text

Package Level Functions fun log(message: String){ println(message) }

Slide 23

Slide 23 text

interface Callback{ fun onComplete() } fun operationWithACallBack(callback: Callback){ Thread.sleep(1000L) callback.onComplete() } Higher order Functions

Slide 24

Slide 24 text

operationWithACallBack(object : Callback{ override fun onComplete() { println("completed") } }) Higher order Functions

Slide 25

Slide 25 text

fun operationWithACallBack(callback: () -> Unit) { Thread.sleep(1000L) callback() } Higher order Functions

Slide 26

Slide 26 text

operationWithACallBack ({ println("completed") }) Higher order Functions

Slide 27

Slide 27 text

operationWithACallBack(){ println("completed") } Higher order Functions

Slide 28

Slide 28 text

operationWithACallBack{ println("completed") } Higher order Functions

Slide 29

Slide 29 text

data class Customer( val name: String, val city: String ) Higher order Functions

Slide 30

Slide 30 text

fun printCustomerByCity(city: String) { val filteredCustomers = customers.filter { it.city == city } filteredCustomers.forEach { println(it) } } fun printCustomerByName(name: String) { val filteredCustomers = customers.filter { it.name == name } filteredCustomers.forEach { println(it) } } Higher order Functions

Slide 31

Slide 31 text

fun printCustomers(predicate: (Customer) -> Boolean) { val filteredCustomer = customers.filter(predicate) filteredCustomers.forEach { println(it) } } fun main(args: Array) { printCustomers { it.city == "Istanbul" } printCustomers { it.name == "Hadi" } } Higher order Functions

Slide 32

Slide 32 text

object StringUtils { fun getThirdCharacter(string: String): Char { if(string.length>2){ return string[2] } throw IllegalArgumentException() } } Extension Functions

Slide 33

Slide 33 text

val thirdLetter = StringUtils.getThirdCharacter("Kotlin") Extension Functions

Slide 34

Slide 34 text

fun String.getThirdCharacter(): Char { if(length>2){ return get(2) } throw IllegalArgumentException() } Extension Functions

Slide 35

Slide 35 text

val thirdCharacter = "Kotlin".getThirdCharacter() Extension Functions

Slide 36

Slide 36 text

list.forEach { println(it) } Extension Functions

Slide 37

Slide 37 text

public inline fun Iterable.forEach(action: (T) -> Unit): Unit { for (element in this) action(element) } Extension Functions

Slide 38

Slide 38 text

Arrow Λrrow is a library for Typed Functional Programming in Kotlin. https://arrow-kt.io

Slide 39

Slide 39 text

Arrow Arrow aims to provide a lingua franca of interfaces and abstractions across Kotlin libraries. For this, it includes the most popular data types, type classes and abstractions such as Option, Either, IO, Functor, Applicative, Monad to empower users to write pure FP apps and libraries built atop higher order abstractions. https://arrow-kt.io

Slide 40

Slide 40 text

fun tryGetThirdCharacter(string:String): Try { return Try { if(string.length<2){ throw IllegalArgumentException("string is short") }else{ string[3] } } } Try

Slide 41

Slide 41 text

tryGetThirdCharacer("Kotlin") .getOrElse { println(it.message) } Try

Slide 42

Slide 42 text

val thirdCharacter = tryGetThirdLetter("Go") .getOrDefault { "a" } Try

Slide 43

Slide 43 text

tryGetThirdLetter("Kotlin") .fold( { println(it.message) }, { println("third char is $it") } ) Try

Slide 44

Slide 44 text

fun getMessage(useResource: Boolean): Either { return if (useResource) { Either.right(R.string.start) } else { Either.left("Start") } } Either

Slide 45

Slide 45 text

val textEither = getMessage(useResource) val text: String = when (textEither) { is Either.Left -> textEither.a is Either.Right -> getString(textEither.b) } Either

Slide 46

Slide 46 text

fun divide(x: Int, y: Int): Option { return if (y == 0) { Option.empty() } else { Option.just(x / y) } } Option

Slide 47

Slide 47 text

val result = divide(x,y).map { it*6 } Option

Slide 48

Slide 48 text

Thank you