Skip to content

Instantly share code, notes, and snippets.

View nomisRev's full-sized avatar
🏂

Simon Vergauwen nomisRev

🏂
View GitHub Profile
@nomisRev
nomisRev / transactionEither.kt
Last active August 10, 2022 08:41
Either-Syntax for SqlDelight transactions
import arrow.continuations.Effect
import arrow.continuations.generic.DelimitedScope
import arrow.core.Either
import arrow.core.right
import com.squareup.sqldelight.TransactionWithReturn
import kotlin.coroutines.RestrictsSuspension
/**
* Either-Syntax for SqlDelight transactions.
*
@nomisRev
nomisRev / healthCheck.kt
Created May 31, 2021 11:42
Postgres health check that checks buffer hit performance
data class HealthCheck(val heapRead: Long, val heapHit: Long, val ratio: Float)
suspend fun DataSource.databaseHealth(): HealthCheck? =
connection.use { conn ->
conn.createStatement()
.executeQuery(healthCheckQuery).use { rs ->
if (rs.next()) HealthCheck(
rs.getLong("heap_read"),
rs.getLong("heap_hit"),
@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 / Example.kt
Last active March 9, 2021 07:22
EitherEffect require syntax
import arrow.core.Either
import arrow.core.computations.EitherEffect
import arrow.core.computations.either
suspend fun <E> EitherEffect<E, *>.require(test: Boolean, fail: () -> E): Unit =
if (test) Unit
else Either.Left(fail()).bind()
suspend fun <E> EitherEffect<E, *>.error(error: E): Unit =
Either.Left(error).bind()
@nomisRev
nomisRev / CompletableFuture.kt
Last active November 9, 2020 11:55
Arrow Fx Coroutines CompletableFuture
import arrow.fx.coroutines.CancelToken
import arrow.fx.coroutines.SuspendConnection
import arrow.fx.coroutines.cancellable
import java.util.concurrent.CancellationException
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionException
import java.util.concurrent.CompletionStage
import java.util.concurrent.ExecutionException
import java.util.concurrent.Future
import kotlin.coroutines.Continuation
@nomisRev
nomisRev / SuspendedQueue.kt
Created July 6, 2020 16:19
Suspended Queue
package arrow.fx.coroutines
import arrow.fx.coroutines.Queue.BackpressureStrategy
import arrow.fx.coroutines.Queue.BackpressureStrategy.*
/**
* [Dequeue] allows peeking and taking values from a [Queue], but doesn't allow offering values to the [Queue].
* You can use [Dequeue] to restrict certain functions or layers of your applications to only consume values.
*
* ```kotlin:ank:playground
@nomisRev
nomisRev / Example.kt
Last active September 13, 2021 22:48
Kotlin DI with receivers & interface delegation
import arrow.core.*
import memeid.UUID
data class User(val email: String, val name: String) {
companion object
}
data class ProcessedUser(val id: UUID, val email: String, val name: String) {
companion object
}
@nomisRev
nomisRev / CircuitBreaker.kt
Created April 15, 2020 10:11
Simple CircuitBreaker example with Kotlin & Arrow Fx
import arrow.fx.extensions.io.applicative.map
import arrow.fx.extensions.io.monad.flatMap
import arrow.fx.extensions.io.monadDefer.monadDefer
import java.lang.RuntimeException
enum class CBState {
CLOSED, OPEN, DISABLED;
}
class CallNotPermitedException(val state: CBState):
@nomisRev
nomisRev / chunks.kt
Created March 27, 2020 10:23
Process chunks with IO - Question on KotlinLang Slack
import arrow.fx.IO
import arrow.fx.extensions.fx
fun chunks(): List<List<String>> = listOf(
listOf("a", "b", "c"),
listOf("d", "e", "g")
)
fun process(prevChunks: List<String>, rest: List<List<String>>, merged: List<String>, size: Int, max: Int): IO<List<String>> =
if (size == max) IO.just(merged)
@nomisRev
nomisRev / EventBus.kt
Created March 27, 2020 10:22
EventBus, question on KotlinLang Slack
import arrow.Kind
import arrow.fx.ForIO
import arrow.fx.IO
import arrow.fx.IOOf
import arrow.fx.Queue
import arrow.fx.extensions.fx
import arrow.fx.extensions.io.concurrent.concurrent
import arrow.fx.extensions.io.dispatchers.dispatchers
import arrow.fx.fix
import arrow.fx.typeclasses.Concurrent