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

KotlinConf - Arrow's 2.0 Trajectory

KotlinConf - Arrow's 2.0 Trajectory

Since the first commit 10 years ago Arrow has travelled a long path to reach 2.0, this talk will take us through the journey, decisions, and challenges that shaped Arrow.

Arrow aims to bring functional programming patterns to Kotlin in an idiomatic way, by leveraging Kotlin's DSL features to represent complex programs in elegant ways.

Simon Vergauwen

April 16, 2023
Tweet

More Decks by Simon Vergauwen

Other Decks in Programming

Transcript

  1. Arrow’s History Oct 2017 March 2017 FineCinnamon/Katz Dec 2017 arrow-kt

    June 2017 Kategory March 2013 MarioAriasC/funKTionale Sept 2017 nomisRev/optikal ΛRROW
  2. Complexity of Higher-Kinded Types interface GistsApi<F> { fun publicGistsForUser(username: String):

    Kind<F, ListK<Gist > > } interface Logger<F> { fun log(msg: String): Kind<F, Unit> } ΛRROW
  3. Complexity of Higher-Kinded Types fun main() { GistsApi(MonoK.async(), MonoKLogger, HttpClient.newHttpClient())

    .publicGistsForUser("nomisRev") .fix() .mono .block() .let( :: println) } ΛRROW
  4. Complexity of Higher-Kinded Types fun main() { GistsApi(IO.async(), ... )

    // Arrow Fx IO GistsApi(DeferredK.async(), ... ) // KotlinX Coroutines Deferred GistsApi(SingleK.async(), ... ) // RxJava Single } ΛRROW
  5. Suspend to the rescue interface GistsApi { suspend fun publicGistsForUser(username:

    String): List<Gist> } interface Logger { suspend fun log(msg: String): Unit } ΛRROW
  6. Suspend to the rescue fun main() { val api =

    GistApi(Logger.Default, HttpClient.newHttpClient()) runBlocking { api.publicGistsForUser("nomisRev") } mono { api.publicGistsForUser("raulraja") } scope.future { api.publicGistsForUser("serras") } 
 // ... } ΛRROW
  7. Arrow’s History Typeclasses & Higher Kinded Types Rehaul of Arrow

    < 0.12.x 0.12.x Suspend & Kotlin Idiomatic 1.x.x ΛRROW
  8. Arrow’s History Typeclasses & Higher Kinded Types Rehaul of Arrow

    < 0.12.x 0.12.x Suspend & Kotlin Idiomatic Small Uniform APIs & learning curve 1.x.x 2.x.x ΛRROW
  9. DSLs and smart-contracts val res: Either<String, Int> = either {

    val x: Int? = null ensureNotNull(x) { "X was zero" } x + 1 } ΛRROW
  10. Idiomatic API names inline fun Either<A, B>.getOrElse(default: (A) -> B):

    B 
 
 fun Either<A, B>.getOrNull(): B? inline fun Either<A, B>.onRight(action: (right: B) - > Unit): Either<A, B> fun Either<A, B>.leftOrNull(): A? inline fun Either<A, B>.onLeft(action: (left: A) -> Unit): Either<A, B> ΛRROW
  11. Context receivers context(Raise<ProcessingFailed>) fun GithubUser.process(): ProcessedUser { ensure(login.isNotEmpty()) { ProcessingFailed

    } return ProcessedUser(this) } 
 context(Raise<ProcessingFailed>) fun List<GithubUser>.processAll(): List<ProcessedUser> = map { it.process() } ΛRROW
  12. DSLs everywhere suspend fun ResourceScope.dataSource(): HikariDataSource = closeable { HikariDataSource(HikariConfig())

    } suspend fun SagaScope.example(): String = saga({ "Update user" }) { println("Rollback user") } ΛRROW
  13. Binary Size Arrow 0.11.0 Arrow 1.0.0 Arrow 2.0.0 5000 4000

    3000 2000 1000 0 ΛRROW 4,1 MB 3,1 MB 429 KB
  14. IntelliJ @Deprecated( RedundantAPI + "Prefer the new recover API", ReplaceWith("recover

    { a -> f(a).bind() }", "arrow.core.recover") ) inline fun <A, B, C> Either<A, B>.handleErrorWith(f: (A) -> Either<C, B>): Either<C, B> ΛRROW
  15. OpenRewrite plugins { kotlin("jvm") id("org.openrewrite.rewrite") version "5.39.3" } dependencies {

    rewrite(“io.arrow-kt:rewrite-arrow:1.0.0-RC3“) } rewrite { activeRecipe("arrow.RaiseRefactor") } ΛRROW
  16. OpenRewrite ./gradlew :sample:rewriteRun > Task :sample:rewriteRun Validating active recipes Parsing

    sources from project sample All sources parsed, running active recipes: arrow.RaiseRefactor Changes have been made to sample/src/main/kotlin/org/example/example.kt by: arrow.RaiseRefactor org.openrewrite.java.ChangeMethodName: .. . org.openrewrite.java.ChangeType: . .. Please review and commit the results. ΛRROW
  17. ΛRROW Large-scale automated refactoring Experimental Kotlin Support Support JVM &

    commonMain Friendly & helpful community First Arrow recipe OpenRewrite
  18. Visit us at the booth to talk to Arrow contributors

    & Xebia Functional and get discount on courses.
 __