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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: kotlinx-coroutines-core/concurrent/src/internal/LockFreeLinkedList.kt | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- kotlinx-coroutines-core/concurrent/src/internal/LockFreeLinkedList.kt (revision 4dd4e59f6676a45499cd193aa8fd9521013f8a3f) | |
+++ kotlinx-coroutines-core/concurrent/src/internal/LockFreeLinkedList.kt (date 1605704376180) | |
@@ -62,12 +62,19 @@ | |
*/ | |
@Suppress("LeakingThis") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
kexe="$1" | |
if [ "$1" == "" ]; then | |
echo "Usage bisect_leaks.sh <path-to-test.kexe>" | |
exit | |
fi | |
echo "Bisecting leaks for $kexe" | |
log="$kexe.log" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.* | |
import kotlinx.coroutines.flow.* | |
private class Buffer<T> { | |
private val list = arrayListOf<T>() | |
@Synchronized | |
fun add(value: T) { list.add(value) } | |
@Synchronized |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://codeforces.com/contest/1297/problem/I | |
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() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |