Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. Introduction to Functional Programing in Kotlin and Arrow Hadi Tok

    Google Developers Expert (Android) Technical Lead, @CitizenMe https://haditok.com/
  2. 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/
  3. 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.
  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.
  5. 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.
  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. 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.
  8. 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?
  9. Pure functions • Its return value is the same for

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

    reference arguments • Throwing an exception • I/O operations
  11. 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
  12. 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
  13. object StringUtils { fun getThirdCharacter(string: String): Char { if(string.length>2){ return

    string[2] } throw IllegalArgumentException() } } Extension Functions
  14. public inline fun <T> Iterable<T>.forEach(action: (T) -> Unit): Unit {

    for (element in this) action(element) } Extension Functions
  15. 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
  16. fun getMessage(useResource: Boolean): Either<String, Int> { return if (useResource) {

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

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

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