May 26, 2020
130

# Introduction to Functional Programing in Kotlin and Arrow

May 26, 2020

## Transcript

1. Welcome

2. Introduction to Functional
Programing in Kotlin and
Arrow

3. Functional
Programming

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.

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

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

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

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): List {
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){
val iterator=list.iterator()
for(i in iterator){
if(i%2==0){
iterator.remove()
}
}
}

16. Mutable data
val filteredList = list.filter { it % 2 != 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

20. Kotlin and Functional
Programming

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

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

23. interface Callback{ fun onComplete() }
fun operationWithACallBack(callback: Callback){
callback.onComplete()
}
Higher order
Functions

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

25. fun operationWithACallBack(callback: () -> Unit) {
callback()
}
Higher order
Functions

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

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

28. operationWithACallBack{
println("completed")
}
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) {
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

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

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

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

36. list.forEach { println(it) }
Extension
Functions

37. public inline fun Iterable.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 {
return Try {
if(string.length<2){
throw IllegalArgumentException("string is short")
}else{
string[3]
}
}
}
Try

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

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

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

44. fun getMessage(useResource: Boolean): Either {
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 {
return if (y == 0) {
Option.empty()
} else {
Option.just(x / y)
}
}
Option

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

48. Thank you