Skip to content

Instantly share code, notes, and snippets.

View elizarov's full-sized avatar

Roman Elizarov elizarov

View GitHub Profile
@elizarov
elizarov / ReentrantMutex.kt
Created April 23, 2021 09:59
ReentrantMutex implementation for Kotlin Coroutines
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
sealed class Expression<A> {
abstract val value: A
}
data class Num(override val value: Int) : Expression<Int>()
data class Bool(override val value: Boolean) : Expression<Boolean>()
data class Add(val a: Expression<Int>, val b: Expression<Int>) : Expression<Int>() {
override val value: Int get() = a.value + b.value
}
data class Equals<A>(val first: Expression<A>, val second: Expression<A>) : Expression<Boolean>() {
override val value: Boolean get() = first.value == second.value
@elizarov
elizarov / DeepRecursiveFunction.kt
Last active March 25, 2024 00:40
Defines recursive function that keeps its stack on the heap (productized version)
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
/**
* Defines deep recursive function that keeps its stack on the heap,
* which allows very deep recursive computations that do not use the actual call stack.
* To initiate a call to this deep recursive function use its [invoke] function.
* As a rule of thumb, it should be used if recursion goes deeper than a thousand calls.
*
* The [DeepRecursiveFunction] takes one parameter of type [T] and returns a result of type [R].
// UPDATE: Everyone finding this gist via Google!
// Modern kotlinx.coroutines has out-of-the-box support for asyncLazy with the following expression:
// val myLazyValue = async(start = CoroutineStart.LAZY) { ... }
// Use myLazyValue.await() when you need it
// ---------------- public api ----------------
public interface AsyncLazy<out T> {
public suspend fun value(): T
public fun isInitialized(): Boolean
@elizarov
elizarov / test.cs
Created September 6, 2021 09:54
Hard exhaustiveness instance in C#
class Test {
int f(int x0, int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9) {
return (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) switch {
(>= 78, >= 80, >= 83, >= 62, < 90, >= 32, < 84, < 77, >= 51, >= 8) => 0,
(< 64, >= 85, >= 7, < 98, >= 71, < 26, >= 37, >= 34, < 5, < 39) => 1,
(>= 23, >= 56, >= 36, >= 64, < 27, < 72, >= 67, >= 28, >= 93, >= 73) => 2,
(>= 35, < 27, >= 71, >= 79, < 95, < 11, >= 86, < 32, < 32, >= 78) => 3,
(< 86, >= 83, < 90, < 99, < 35, < 6, < 69, < 80, < 4, < 85) => 4,
(< 38, >= 77, >= 70, >= 0, >= 9, < 96, < 5, < 39, >= 20, < 25) => 5,
(< 46, >= 71, >= 74, < 2, < 79, < 56, >= 34, < 8, < 53, >= 36) => 6,
@elizarov
elizarov / Delimited.kt
Last active October 23, 2023 20:19
Delimited Continuations shift/reset in Kotlin
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
/**
* Implementation for Delimited Continuations `shift`/`reset` primitives via Kotlin Coroutines.
* See [https://en.wikipedia.org/wiki/Delimited_continuation].
*
* The following LISP code:
*
* ```
@elizarov
elizarov / PauseableTimeout.kt
Created November 8, 2019 08:41
A version of withTimeoutOrNull that can be paused/resumed.
import kotlinx.coroutines.*
import kotlin.coroutines.*
/**
* Scope interface to control (pause & resume) timeout
*/
interface TimerScope : CoroutineScope {
fun pause()
fun resume()
}
@elizarov
elizarov / MultiShotEnumeration.kt
Created May 3, 2017 09:01
Showcase for Kotlin multishot continuations
import kotlin.coroutines.experimental.*
import kotlin.coroutines.experimental.intrinsics.*
fun main(args: Array<String>) {
enumerate {
if (flip("A")) {
if (flip("B")) 1 else 2
} else {
if (flip("C")) 3 else if (flip("D")) 4 else 5
}
@elizarov
elizarov / AD.kt
Last active February 26, 2023 15:18
Automatic Differentiation with Kotlin
/*
* Implementation of backward-mode automatic differentiation.
*/
/**
* Differentiable variable with value and derivative of differentiation ([grad]) result
* with respect to this variable.
*/
data class D(var x: Double, var d: Double = 0.0) {
constructor(x: Int): this(x.toDouble())
@elizarov
elizarov / AspectOperators.kt
Last active September 8, 2022 07:56
Aspect operators
// Aspect interface for combinator
interface Aspect {
operator fun <R> invoke(block: () -> R): R
}
// Aspect combinator
operator fun Aspect.plus(other: Aspect) = object : Aspect {
override fun <R> invoke(block: () -> R): R =
this@plus {
other {