Skip to content

Instantly share code, notes, and snippets.

View nomisRev's full-sized avatar
🏂

Simon Vergauwen nomisRev

🏂
View GitHub Profile
@nomisRev
nomisRev / ContextReceivers.kt
Created April 20, 2022 06:40
Context Receivers lambdas
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")
@nomisRev
nomisRev / KtorOAuth.kt
Created February 23, 2022 08:13
Ktor Google OAuth2
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
@nomisRev
nomisRev / DataSourceSyntax.kt
Last active February 8, 2022 17:48
javax.sql.DataSource Syntax Mix-in for Kotlin
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
@nomisRev
nomisRev / AppleTest.kt
Created January 5, 2022 18:03
Gradle Kotlin MPP appleTest task
// ./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") }
@nomisRev
nomisRev / Validated.kt
Created December 14, 2021 14:50
Example of Arity-18 for Validated
/**
* 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()
@nomisRev
nomisRev / arrowInjekt.kt
Last active November 23, 2021 08:20
Receiver Context Dependency Injection
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
@nomisRev
nomisRev / Query.kt
Last active November 22, 2021 09:03
Either based transaction with Exposed. https://github.com/JetBrains/Exposed
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) }
@nomisRev
nomisRev / Cont.kt
Created October 29, 2021 16:10
Cont<R, A> implementation in Kotlin
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
@nomisRev
nomisRev / List.MD
Created September 21, 2021 15:58
Slack conversations
@nomisRev
nomisRev / gist:2e4f06bcffd01833df612114814eefa0
Created September 21, 2021 15:56
Arrow Computation block `eager` vs `suspend`
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