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

Functional Kotlin with Arrow (jLove 2021)

Functional Kotlin with Arrow (jLove 2021)

What is a monad? Do I need a PhD to write in functional style? How do I do that in Kotlin? And other questions that you were afraid to ask.

In this talk, we'll walk through Arrow Core, the functional companion to the Kotlin standard library. We'll cover both the concepts that every software engineer may add to their project right now and more advanced structures, starting from non-empty list, going over Validated and Either followed by monad comprehension.

Karin-Aleksandra Monoid

June 25, 2021
Tweet

More Decks by Karin-Aleksandra Monoid

Other Decks in Technology

Transcript

  1. About Senior Software Engineer Background in Kotlin & Scala (both

    front and backend) Living in Berlin with two cats
  2. What is Arrow? Core (more functional programming) FX (functional effects)

    Optics (for traversing and transforming immutable data structures) Meta (writing compiler plugins) FX Optics Meta Core
  3. What is Arrow? Core (more functional programming) FX (functional effects)

    Optics (for traversing and transforming immutable data structures) Meta (writing compiler plugins) FX Optics Meta Core
  4. Non-empty list Payment fields ​Type Is Required? Phone​ String(100)​ -

    Reference​ String(100)​ Required​ Amount​ Decimal​ Required​
  5. Non-empty list Is JSON {“payments”: []} ok? 🤔 Payment fields

    ​Type Is Required? Phone​ String(100)​ - Reference​ String(100)​ Required​ Amount​ Decimal​ Required​
  6. Result Type fun deserialize(): Something { return try { doSomething()

    } catch (_: Exception) { try { doSomethingElse() } catch(_: Exception) { doSomethingElseAgain() }}... fun deserialize(): Something { return runCatching { doSomething() }.recoverCatching { doSomethingElse() }.getOrElse { doSomethingElseAgain() } }
  7. Sealed Type sealed class Result class Success : Result() class

    FullMoonFailure : Result() class ElonMuskTweetedFailure : Result()
  8. Either sealed class Either<out A, out B> Which can be:

    o Either.Left<A> o Either.Right<B>
  9. Validated sealed class Validated<out A, out B> Which can be:

    o Validated.Invalid<A> o Validated.Valid<B>
  10. Validated vs Either o Either stops at first fail, validated

    collects all: User(phone, name, age) User(“test”, XÆA-12 Musk, -10) o Either: invalid phone o Validated: invalid phone, name*, age * https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/
  11. Ior sealed class Ior<out A, out B> Which can be:

    o Ior.Left<A> o Ior.Right<B> o Ior.Both<A,B>
  12. Chaining - flatMap val data = getFromDB(...) .flatMap { user

    -> getFromDB(...).flatMap { stockInfo-> getFromDB(...).flatMap { historicalData-> PageInfo(...) }}}}}}}}} private fun <T> getFromDB(param: String): Either<ErrorResponse, T>
  13. Chaining - monad comprehensions val data = either { val

    user = getFromDB(request.userId).bind() val stockInfo = getFromDB(request.stockId).bind() val historicalData = getFromDB(request.stockId).bind() // ... PageInfo(user, stockInfo, historicalData, ...) } return data private fun <T> getFromDB(param: String): Either<ErrorResponse, T>
  14. Next steps o Core: Option, Eval, extentions o FX: suspend,

    parZip, parTraverse, raceN... o Optics