Skip to content

Instantly share code, notes, and snippets.

Avatar

Simon Vergauwen nomisRev

View GitHub Profile
@nomisRev
nomisRev / List.MD
Created Sep 21, 2021
Slack conversations
View List.MD
@nomisRev
nomisRev / gist:2e4f06bcffd01833df612114814eefa0
Created Sep 21, 2021
Arrow Computation block `eager` vs `suspend`
View gist:2e4f06bcffd01833df612114814eefa0
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
@nomisRev
nomisRev / CopurityInKotlin.md
Created Aug 27, 2021
Collection of small explanations I gave on Slack.
View CopurityInKotlin.md

In Kotlin there is the concept of “co-pure”. The Kotlin compiler has a powerful inliner, which is very commonly used in Kotlin. An inliner takes the code from an inline fun, and puts it in the place of the call-site. So if an inline fun is pure in it’s definition, and it takes a lambda then the resulting code is pure if and only if the passed lambda is also pure. Otherwise the resulting code is impure. So the whether the resulting code pure or not depends 100% on the passed lambda.

fun pureExample(): Unit = 
  listOf(1, 2, 3).map { it + 1 } // [2, 3, 4] & pure
@nomisRev
nomisRev / ResourcePool.kt
Created Aug 15, 2021
Arrow Fx Resource Pool
View ResourcePool.kt
/**
* Creates a pool with [n] resouces of [A].
* It creates [n] resources of
*/
fun <A> Resource<A>.pool(n: Int): Resource<ClusteredResources<A>> =
List(n) { this }.sequence()
.flatMap { all ->
Resource({
ClusteredResources(
Channel<A>(all.size).apply {
@nomisRev
nomisRev / testJar.gradle
Created Jul 28, 2021
Main-jar & Test-jar from regular code
View testJar.gradle
// PROJECT A
val testsConfig by configurations.creating {
extendsFrom(configurations["testImplementation"])
}
dependencies {
testsConfig(sourceSets.getByName("test").output)
}
// PROJECT B
testImplementation(project(":projectA", "testsConfig"))
@nomisRev
nomisRev / DataSourceSyntax.kt
Last active Jul 25, 2021
javax.sql.DataSource Syntax Mix-in for Kotlin
View DataSourceSyntax.kt
package com.github.nomisrev
import arrow.fx.coroutines.Resource
import arrow.fx.coroutines.fromAutoClosable
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Types
import javax.sql.DataSource
@nomisRev
nomisRev / transactionEither.kt
Last active Jul 27, 2021
Either-Syntax for SqlDelight transactions
View transactionEither.kt
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
Postgres health check that checks buffer hit performance
View healthCheck.kt
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 / Example.kt
Last active Mar 9, 2021
EitherEffect require syntax
View Example.kt
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 Nov 9, 2020
Arrow Fx Coroutines CompletableFuture
View CompletableFuture.kt
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