$30 off During Our Annual Pro Sale. View Details »

Introduction to Functional Programing in Kotlin and Arrow

Introduction to Functional Programing in Kotlin and Arrow

Hadi Tok

May 26, 2020
Tweet

More Decks by Hadi Tok

Other Decks in Technology

Transcript

  1. Welcome

    View Slide

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

    View Slide

  3. Functional
    Programming

    View Slide

  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/

    View Slide

  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.

    View Slide

  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.

    View Slide

  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.

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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.

    View Slide

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

    View Slide

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

    View Slide

  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.

    View Slide

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

    View Slide

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

    View Slide

  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?

    View Slide

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

    View Slide

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

    View Slide

  20. Kotlin and Functional
    Programming

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  48. Thank you

    View Slide