- Creating computation DSL by weaving suspend functions: https://kotlinlang.slack.com/archives/C5UPMM0A0/p1631326397023700
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 kotlin.contracts.ExperimentalContracts | |
import kotlin.contracts.InvocationKind | |
import kotlin.contracts.contract | |
import kotlin.time.ExperimentalTime | |
import kotlin.time.measureTimedValue | |
interface Logging { | |
fun info(msg: String): Unit | |
companion object Default : Logging { | |
override fun info(msg: String) = println("INFO: $msg") |
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 io.ktor.client.HttpClient | |
import io.ktor.client.call.body | |
import io.ktor.client.engine.cio.CIO | |
import io.ktor.client.request.get | |
import io.ktor.client.request.headers | |
import io.ktor.http.HttpHeaders | |
import io.ktor.http.HttpMethod | |
import io.ktor.http.HttpStatusCode | |
import io.ktor.server.application.call | |
import io.ktor.server.application.Application |
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
package com.github.nomisrev | |
import arrow.fx.coroutines.Resource | |
import arrow.fx.coroutines.fromAutoCloseable | |
import java.sql.Connection | |
import java.sql.PreparedStatement | |
import java.sql.ResultSet | |
import java.sql.Types | |
import javax.sql.DataSource |
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
// ./gradlew runs tests for all configured Apple targets | |
val appleTest = tasks.create("appleTest") | |
subprojects { | |
afterEvaluate { | |
val appleTargets = setOf("tvos", "watchos", "ios", "macos") | |
extensions.findByType<org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension>() | |
?.sourceSets | |
?.filter { appleTargets.any { target -> it.name.contains(target) } && it.name.contains("Test") } |
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
/** | |
* In Arrow we only provide functions up to arity-9, | |
* using Tuple we can easily compose up to arity-n. | |
* | |
* A small example to show Validated#zip for arity-18 | |
*/ | |
fun validated(): ValidatedNel<String, Int> = | |
"example".invalidNel() |
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
package com.example.sample | |
import arrow.core.* | |
import arrow.core.computations.either | |
import arrow.fx.coroutines.raceN | |
public data class NewsItem(val id: String) | |
public data class Detail(val id: String, val content: String) | |
public object Failure |
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.Either | |
import arrow.core.computations.EitherEffect | |
import arrow.core.computations.either | |
import kotlinx.coroutines.Dispatchers | |
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction | |
typealias Query<E, A> = suspend () -> Either<E, A> | |
fun <E, A> query(f: suspend EitherEffect<E, *>.() -> A): Query<E, A> = | |
suspend { either(f) } |
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.identity | |
import kotlin.coroutines.Continuation | |
import kotlin.coroutines.resume | |
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn | |
import kotlin.coroutines.intrinsics.startCoroutineUninterceptedOrReturn | |
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED | |
interface ContEffect<R, A> { | |
suspend fun <B> shift(r: R): B |
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
The DSL uses the suspension system, but the Restricted version uses @RestrictSuspension. | |
This disallows any suspend function to be called inside the DSL except for the functions defined inside RestrictOptionEffect. | |
This means that the DSL can be evaluated immediately, or eagerly. Instead of requiring suspend. | |
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/-restricts-suspension/ | |
If you’re already inside a suspend context then it makes most sense to just use the suspend version. That’ll also allow you to use other suspending code inside the DSL |