Last active
October 14, 2020 09:00
-
-
Save fteychene/53803ccb668711672bce476bcd45d2e7 to your computer and use it in GitHub Desktop.
Arrow fx coroutine play on Chuck Norris facts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
id 'org.jetbrains.kotlin.jvm' version '1.4.10' | |
} | |
apply plugin: 'kotlin-kapt' | |
group 'org.example' | |
version '1.0-SNAPSHOT' | |
repositories { | |
mavenCentral() | |
jcenter() | |
maven { url "https://dl.bintray.com/arrow-kt/arrow-kt/" } | |
maven { url 'https://oss.jfrog.org/artifactory/oss-snapshot-local/' } // for SNAPSHOT builds | |
} | |
def arrow_version = "0.11.0" | |
def fuel_version = "2.3.0" | |
dependencies { | |
implementation "org.jetbrains.kotlin:kotlin-stdlib" | |
implementation "io.arrow-kt:arrow-core:$arrow_version" | |
implementation "io.arrow-kt:arrow-fx:$arrow_version" | |
implementation "io.arrow-kt:arrow-fx-coroutines:$arrow_version" | |
implementation "io.arrow-kt:arrow-optics:$arrow_version" | |
implementation "io.arrow-kt:arrow-syntax:$arrow_version" | |
kapt "io.arrow-kt:arrow-meta:$arrow_version" | |
implementation "com.github.kittinunf.fuel:fuel:$fuel_version" | |
implementation "com.github.kittinunf.fuel:fuel-jackson:$fuel_version" | |
implementation "com.github.kittinunf.fuel:fuel-coroutines:$fuel_version" | |
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.11.+" | |
} | |
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { | |
kotlinOptions { | |
jvmTarget = "1.8" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arrow.core.* | |
import arrow.core.extensions.either.applicative.applicative | |
import arrow.core.extensions.either.applicative.map | |
import arrow.core.extensions.list.traverse.sequence | |
import arrow.core.extensions.listk.functor.functor | |
import arrow.fx.IODispatchers.IOPool | |
import arrow.fx.coroutines.ComputationPool | |
import arrow.fx.coroutines.evalOn | |
import arrow.fx.coroutines.parTraverse | |
import arrow.typeclasses.Show | |
import com.github.kittinunf.fuel.core.awaitResponseResult | |
import com.github.kittinunf.fuel.httpGet | |
import com.github.kittinunf.fuel.jackson.jacksonDeserializerOf | |
suspend fun printThreadName(): Unit = | |
println(Thread.currentThread().name) | |
data class HttpError(val message: String, val cause: Throwable?) | |
typealias Category = String | |
suspend fun loadCategories(): Either<HttpError, List<Category>> = | |
evalOn(IOPool) { | |
val (value, error) = "https://api.chucknorris.io/jokes/categories" | |
.httpGet() | |
.awaitResponseResult(jacksonDeserializerOf<List<String>>()).third | |
printThreadName() | |
error | |
?.let { Either.left(HttpError("Error loading categories", it)) } | |
?: Either.right(value!!) | |
} | |
data class Quote( | |
val value: String | |
) | |
suspend fun loadRandom(category: String): Either<HttpError, Quote> = | |
evalOn(IOPool) { | |
val (value, error) = "https://api.chucknorris.io/jokes/random?category=$category" | |
.httpGet() | |
.awaitResponseResult(jacksonDeserializerOf<Quote>()).third | |
printThreadName() | |
error | |
?.let { Either.left(HttpError("Error loading quote for $category", it)) } | |
?: Either.right(value!!) | |
} | |
suspend fun loadQuotesForCategories(categories: List<Category>): Either<HttpError, List<Pair<Category, Quote>>> = | |
run { | |
printThreadName() | |
categories.parTraverse { category -> loadRandom(category).map { category to it } } | |
.sequence(Either.applicative()).map { it.fix() }.fix() | |
} | |
val categoryQuoteShow = object : Show<Pair<Category, Quote>> { | |
override fun Pair<Category, Quote>.show(): String = | |
"$first - ${second.value}" | |
} | |
val showList = with(categoryQuoteShow) { | |
ListK.functor().lift<Pair<Category, Quote>, String> { it.show() } | |
} | |
suspend fun main() { | |
evalOn(ComputationPool) { | |
printThreadName() | |
loadCategories() | |
.flatMap { loadQuotesForCategories(it) } | |
.map { showList.invoke(it.k()).fix() } | |
.map { | |
printThreadName() | |
it.forEach(::println) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output sample