Skip to content

Instantly share code, notes, and snippets.


Roman Elizarov elizarov

View GitHub Profile
elizarov / dump-cycles.patch
Created Nov 18, 2020
Dump cycles for kotinx-coroutines 1.4.1 running on Kotlin 1.4.20
View dump-cycles.patch
Index: kotlinx-coroutines-core/concurrent/src/internal/LockFreeLinkedList.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
--- kotlinx-coroutines-core/concurrent/src/internal/LockFreeLinkedList.kt (revision 4dd4e59f6676a45499cd193aa8fd9521013f8a3f)
+++ kotlinx-coroutines-core/concurrent/src/internal/LockFreeLinkedList.kt (date 1605704376180)
@@ -62,12 +62,19 @@
elizarov /
Created Nov 18, 2020
Bisect leaks in K/N code
if [ "$1" == "" ]; then
echo "Usage <path-to-test.kexe>"
echo "Bisecting leaks for $kexe"
elizarov / Main.kt
Created Jul 3, 2020
Flow.asPublisher stress-test with concurrent request/onNext
View Main.kt
import kotlin.coroutines.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.reactive.*
import org.reactivestreams.*
import java.util.concurrent.*
import java.util.concurrent.atomic.*
import kotlin.random.*
View BlockingInputFlow.kt
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking<Unit> {
// working in the main thread
val stdinFlow = System.`in`.bufferedReader().lineSequence().asFlow()
// lets launch a coroutine that collects this flow and prints lines:
stdinFlow.onEach { println(it) }.launchIn(this)
// is the main thread active or blocked?
println("I'm not blocked yet")
elizarov / BlindfoldedBullseye.kt
Last active Apr 20, 2020
GCJ 2020: Blindfolded Bullseye
View BlindfoldedBullseye.kt
fun main() {
val t = readLine()!!.split(" ")[0].toInt()
val s = 1_000_000_000
val n = 9
val d = (2 * s) / (n + 1)
fun hit(i: Int): Int = -s + i * d
fun ask(x: Int, y: Int, swap: Boolean = false): Boolean? {
println(if (swap) "$y $x" else "$x $y")
return when (readLine()!!) {
"CENTER" -> null
View TimedBuffer.kt
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
private class Buffer<T> {
private val list = arrayListOf<T>()
fun add(value: T) { list.add(value) }
elizarov / KH3I.kt
Created Feb 28, 2020
Kotlin Heroes: Episode 3 - Solution for Problem I. Falling Blocks
View KH3I.kt
import java.util.*
private class Block(val l: Int, val r: Int) : Comparable<Block> {
fun cover(c: Block) = l <= c.l && r >= c.r
override fun compareTo(other: Block): Int = l.compareTo(other.l)
fun main() {
val (n, d) = readLine()!!.split(" ").map { it.toInt() }
elizarov / StrategyFinder.kt
Last active Jan 30, 2020
Math puzzle strategy finder
View StrategyFinder.kt
import kotlin.math.*
Player A gets N random coin flips.
Player B gets M random coin flips.
They don't see each other coins, but has agreed on strategy in advance:
- A names the # of B's coin
- B names the # of A's coin
If both their named rolls agree they win, otherwise they loose.
Question: What is their best strategy and chances for winning?
elizarov /
Created Jan 24, 2020
RFC: Flow: Optional backpressure-handling strategy in buffer operator

Flow has buffer(capacity: Int) operator to adapt fast consumers to slow producers. It does offer some flexibility and supports buffer(Channel.CONFLATED), but it does not cover one important use-case that happens in practice. Consider a flow of UI events (like keyboard/mouse clicks). If they go too fast and the application is not keeping up with their processing it would be better to skip the events altogether at some point than to buffer them indefinitely and process later. When to drop/skip? There might be multiple strategies based on time-limit or buffer-size. It seems pretty natural to support at least some of those strategies directly in buffer operator.

View TypesafeEval.kt
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
You can’t perform that action at this time.