Skip to content

Instantly share code, notes, and snippets.

View elizarov's full-sized avatar

Roman Elizarov elizarov

View GitHub Profile
@elizarov
elizarov / Scratch.kt
Created June 23, 2022 07:22
List display with contexts
interface Display<in F, in A> {
context(SingleDisplay<A>) fun display(f: F): String
}
interface SingleDisplay<T> {
fun display(f: T): String
}
object IntDisplay : SingleDisplay<Int> {
override fun display(f: Int): String = "Display Int $f"
@elizarov
elizarov / SequenceSerializationTest.kt
Last active February 2, 2022 10:17
Trying to serialization sequence coroutine state with Java Serialization
import java.io.*
fun main() {
// test sequence -- 15 fibonacci numbers
val seq = sequence {
var prev = 0
var cur = 1
repeat(15) {
yield(cur)
cur += prev.also { prev = cur }
@elizarov
elizarov / Test.kt
Created October 5, 2021 06:32
KT-46872
import java.io.*
import java.lang.NullPointerException
data class A(val s: String?) : Serializable {
fun add(b: B) {
bs.add(b)
}
// we'll add B(this) to 'bs'
private val bs = mutableSetOf<B>()
// check is B(this) is in 'bs'
@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 / 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
import kotlin.math.roundToInt
import kotlin.time.ExperimentalTime
import kotlin.time.TimeSource
private const val MOD = 998244353
private fun mul(a: Int, b: Int) = (a.toLong() * b % MOD).toInt()
@OptIn(ExperimentalTime::class)
fun main() {
@file:Suppress("RESERVED_VAR_PROPERTY_OF_VALUE_CLASS")
open class EntityFactory<E>(val size: Int, val factory: (Int) -> E)
class EntityContext {
var d = DoubleArray(16)
private set
var size: Int = 0
private set
fun <E> create(entity: EntityFactory<E>): E {
interface Group<T> {
val id: T // identity
operator fun T.plus(that: T): T // group operation
operator fun T.unaryMinus(): T // inverse
}
inline class Residue(val x: Int)
// Group of residues modulo n
class Mod(val n: Int): Group<Residue> {
@elizarov
elizarov / Twist.scad
Last active January 17, 2021 08:59
Twisted vase / pen holder
$fs = $preview ? 1 : 0.2;
$fa = $preview ? 3 : 1.0;
rb = 30; // radius of the base
rf = 3; // radius of the side supports
rg = 2; // radius of the cutout groove
n = 10; // number of support twisting in one direcation (total - twice this)
h = 90; // height (mm)
t = 180; // twist anle (degree)
@elizarov
elizarov / FlowTransformOptimizations.kt
Created December 26, 2020 09:36
FlowTransformOptimizations
// see https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/common/src/flow/operators/Transform.kt#L20
public inline fun <T> Flow<T>.filter(crossinline predicate: suspend (T) -> Boolean): Flow<T> = transform { value ->
if (predicate(value)) return@transform emit(value)
}
// see https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/common/src/flow/operators/Transform.kt#L47
public inline fun <T, R> Flow<T>.map(crossinline transform: suspend (value: T) -> R): Flow<R> = transform { value ->
return@transform emit(transform(value))
}