Skip to content

Instantly share code, notes, and snippets.

View nomisRev's full-sized avatar
🏂

Simon Vergauwen nomisRev

🏂
View GitHub Profile
package io.github.nomisrev
import app.cash.sqldelight.driver.jdbc.asJdbcDriver
import arrow.continuations.SuspendApp
import arrow.continuations.ktor.server
import arrow.fx.coroutines.autoCloseable
import arrow.fx.coroutines.closeable
import arrow.fx.coroutines.resourceScope
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
@nomisRev
nomisRev / AutoClose.kt
Last active July 19, 2023 13:03
A Kotlin DSL for AutoCloseable
import arrow.atomic.Atomic
import arrow.atomic.update
/**
* AutoClose offers DSL style API for creating parent-child relationships of AutoCloseable dependencies
*/
interface AutoClose : AutoCloseable {
fun <A : AutoCloseable> autoClose(autoCloseable: A): A
}
@nomisRev
nomisRev / Example.kt
Created April 10, 2023 16:55
StackOverflow Question
import arrow.core.Nel
import arrow.core.raise.Raise
import arrow.core.raise.recover
import arrow.fx.coroutines.parZipOrAccumulate
object ApplicationError
object UserLegalId
object User
object DepartmentCode
object Department
@nomisRev
nomisRev / Racers.kt
Last active February 27, 2023 19:15
POC Race Design
sealed interface RaceRes<A, B>
data class First<A>(val first: A): RaceRes<A, Nothing>
data class FirstWithFailure<A>(val first: A, val second: Throwable): RaceRes<A, Nothing>
data class Second<B>(val second: B): RaceRes<Nothiing, B>
data class SecondWithFailure<B>(val first: Throwable, val second: B): RaceRes<Nothing, B>
/**
* raceSuccess({ req(0) }) { req(0) }).merge()
* What to do with exceptions?? RaceRes seems very clumsy.
*/
@nomisRev
nomisRev / README.MD
Last active August 28, 2023 06:43
Migration script for Arrow 1.2.0 to migrate towards arrow.core.raise.*

Arrow 1.2.0 Raise migration script

This migration script attempts to automatically migrate arrow.core.computations.* and arrow.core.continuations.* on a best effort to arrow.core.raise.*. It has been tested on serveral real-life projects with 100% success, being able to automatically migrate the entire codebase.

The run this kts script you need kotlinc install on your machine. The official documentation on how to install kotlinc.

Some methods like ensure in the DSL became top-level, and fold if you're using Effect or EagerEffect. These new top-level imports cannot be automatically migrated, and there are two ways of dealing with the necessary imports.

@nomisRev
nomisRev / TimedDSL.kt
Last active December 20, 2022 10:46
TimedDSL
import arrow.core.continuations.AtomicRef
import arrow.core.continuations.update
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.TimedValue
import kotlin.time.measureTimedValue
@OptIn(ExperimentalTime::class)
class TimedDSL {
val total: AtomicRef<Duration> = AtomicRef(Duration.ZERO)
@nomisRev
nomisRev / Timed.kt
Created September 21, 2022 07:19
Timed Value monad.
public interface Timed<A> {
public fun result(): A
public fun startMs(): Long
public fun endMs(): Long
}
// Add strategies as needed
public enum class TimedStrategy { EarliestStartAndLatestEnd; }
public interface TimedDSL {
@nomisRev
nomisRev / Console.kt
Created May 23, 2022 15:50
Console Effect handler
import arrow.core.continuations.EffectScope
import arrow.core.continuations.effect
object EndOfLine
interface Console {
context(EffectScope<EndOfLine>)
suspend fun read(): String
suspend fun String.write(): Unit
@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