May 26, 2020
150

# Introduction to Functional Programing in Kotlin and Arrow

May 26, 2020

## Transcript

4. ### 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/
5. ### 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.
6. ### 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.
7. ### 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.

11. ### 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.
12. ### State class MyClass{ var list = listOf(1,2,3,4,5) fun filterList() {

list = list.filter { it%2 !=0 } } }
13. ### State class MyClass{ val list = listOf(1,2,3,4,5) fun filterList(list:List<Int>): List<Int>

{ return list.filter { it!=0 } } }
14. ### 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.
15. ### Mutable data fun filterList(list:List<Int>){ val iterator=list.iterator() for(i in iterator){ if(i%2==0){

iterator.remove() } } }

!= 0 }
17. ### 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?
18. ### Pure functions • Its return value is the same for

the same arguments • Has no side effects
19. ### Side effects • Mutation of local static variables,non-local variables, mutable

reference arguments • Throwing an exception • I/O operations

}

23. ### interface Callback{ fun onComplete() } fun operationWithACallBack(callback: Callback){ Thread.sleep(1000L) callback.onComplete()

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

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

order Functions

29. ### data class Customer( val name: String, val city: String )

Higher order Functions
30. ### 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
31. ### fun printCustomers(predicate: (Customer) -> Boolean) { val filteredCustomer = customers.filter(predicate)

filteredCustomers.forEach { println(it) } } fun main(args: Array<String>) { printCustomers { it.city == "Istanbul" } printCustomers { it.name == "Hadi" } } Higher order Functions
32. ### object StringUtils { fun getThirdCharacter(string: String): Char { if(string.length>2){ return

string[2] } throw IllegalArgumentException() } } Extension Functions

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

} Extension Functions

37. ### public inline fun <T> Iterable<T>.forEach(action: (T) -> Unit): Unit {

for (element in this) action(element) } Extension Functions
38. ### Arrow Λrrow is a library for Typed Functional Programming in

Kotlin. https://arrow-kt.io
39. ### 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
40. ### fun tryGetThirdCharacter(string:String): Try<Char> { return Try { if(string.length<2){ throw IllegalArgumentException("string

is short") }else{ string[3] } } } Try

} ) Try
44. ### fun getMessage(useResource: Boolean): Either<String, Int> { return if (useResource) {

Either.right(R.string.start) } else { Either.left("Start") } } Either
45. ### val textEither = getMessage(useResource) val text: String = when (textEither)

{ is Either.Left -> textEither.a is Either.Right -> getString(textEither.b) } Either
46. ### fun divide(x: Int, y: Int): Option<Int> { return if (y

== 0) { Option.empty() } else { Option.just(x / y) } } Option