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

Kotlin + Arrow = Alchemy

Carlo Corti
November 29, 2019

Kotlin + Arrow = Alchemy

What happens if we mix together Kotlin and Arrow?
The answer is simple: Alchemy.

In this talk, I will deepen the alchemy between Kotlin and Arrow, by taking a procedural program and incrementally refactor to a purely functional one.

Carlo Corti

November 29, 2019
Tweet

More Decks by Carlo Corti

Other Decks in Programming

Transcript

  1. Refactoring “to restructure software by applying a series of refactorings

    without changing its observable behavior” Martin Fowler, Refactoring: Improving the Design of Existing Code
  2. Try<A> Represents a computation that can result in a value

    or in an exception if something has gone wrong. Two possible implementations: • operation has been successful → Success<A> • computation has failed with a Throwable → Failure https://arrow-kt.io/docs/apidocs/arrow-core-data/arrow.core/-try/
  3. It’s an higher order functions that process a data structure

    in some order and build a return value. inline fun <B> fold(ifFailure: (Throwable) -> B, ifSuccess: (A) -> B): B = when (this) { is Failure -> ifFailure(exception) is Success -> ifSuccess(value) } https://arrow-kt.io/docs/apidocs/arrow-core-data/arrow.core/-try/fold.html Fold
  4. IO https://arrow-kt.io/docs/effects/io/ Used to achieve referential transparency for: • side-effects

    • lazy operations that can fail Data type of choice when interacting with: databases, network, operative systems, files
  5. flatMap https://arrow-kt.io/docs/apidocs/arrow-core-data/arrow.core/-option/flat-map.html fun <B> flatMap(f: (A) -> OptionOf<B>): Option<B> =

    when (this) { is None -> this is Some -> f(t).fix() } Returns the result of applying f to this option’s value if this option is nonempty. Returns none if this option is empty
  6. Long Story Short Pure/Total functions Immutability Go for principles not

    abstractions Learn abstractions out of need Category Theory as plus