Skip to content

Instantly share code, notes, and snippets.

Roman Elizarov elizarov

Block or report user

Report or block elizarov

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
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
elizarov /
Last active Dec 26, 2019
StateFlow Design Draft

StateFlow Design Draft

UI applications often need to maintain observable state. The state is conceptually a time-changing variable that always has a value that can be observed by multiple views. Currently, ConflatedBroadcastChannel is used for such state representation but it is not very efficient, because it has to provide full-blown implementation of SendChannel for updating state and ReceiveChannel for observing state, but far simpler concept is actually needed in practice.


elizarov /
Last active Feb 5, 2020
Flow.withState Design Draft

Flow.withState Design Draft

There's a whole set of potential Flow operators that maintain some kind of state for each active collection. For example, Flow.withIndex and conceptually similar operators.


Introduce withState operator to simplify implementation for all kinds of stateful operators:

elizarov /
Created Dec 26, 2019
Flow.withState Design


There's a whole set of potential Flow operators that maintain some kind of state for each active collection. For example, Flow.withIndex and conceptually similar operators.


The proposal is to introduce a generic withState operator to simplify implementation for all of them:

elizarov / PauseableTimeout.kt
Created Nov 8, 2019
A version of withTimeoutOrNull that can be paused/resumed.
View PauseableTimeout.kt
import kotlinx.coroutines.*
import kotlin.coroutines.*
* Scope interface to control (pause & resume) timeout
interface TimerScope : CoroutineScope {
fun pause()
fun resume()
View gist:b24935058a13fb8d1755816bdb4beecd
elizarov / Wrapper.kt
Created Jun 5, 2019
Simple wrapper function combinator engine for Kotlin
View Wrapper.kt
// ============ test functions ============
fun withTransactional() = transactional {
println("withTransactional body")
fun withLogged() = logged {
println("withLogged body")
You can’t perform that action at this time.