Skip to content

Instantly share code, notes, and snippets.

View elizarov's full-sized avatar

Roman Elizarov elizarov

View GitHub Profile
@elizarov
elizarov / A.java
Created January 30, 2019 14:50
Package-private inheritance
package a;
public abstract class A {
abstract void foo();
public void callFooA() {
foo();
}
}
@elizarov
elizarov / Delimited.kt
Last active October 27, 2024 23:38
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 / 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 {
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
// Solution for http://codeforces.com/problemset/problem/767/C
fun main() {
val n = readLine()!!.toInt()
val a = IntArray(n + 1)
val t = IntArray(n + 1)
for (i in 1..n) {
val (ai, ti) = readLine()!!.split(" ").map { it.toInt() }
class Person(val isClever: Boolean)
val p = Person(isClever = true)
// ERROR - missed "false"
val example1 = when (p.isClever) {
true -> "Yes, this person is clever"
}
// No error - all cases matched
@elizarov
elizarov / deferredCache.kt
Created September 27, 2018 09:26
DeferredCache
fun <T : Any> CoroutineScope.deferredCache(block: suspend CoroutineScope.() -> T) : Deferred<T> =
async(start = CoroutineStart.LAZY) {
var result: T
while (true) {
try {
result = block()
break
} catch (e: Throwable) {
// failed -- retry
// todo: log exception somehow?
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.rx1.awaitFirst
import kotlinx.coroutines.experimental.rx1.rxObservable
import java.util.concurrent.TimeUnit
fun fetchFromNetwork() = rxObservable {
delay(1, TimeUnit.HOURS)
send("Result from network")
}
import java.util.stream.Stream
abstract class NodeScaffold<THIS : NodeScaffold<THIS>> {
private val children: List<THIS>? = null
fun children(): Stream<THIS> {
return children!!.stream()
}
@elizarov
elizarov / KT24481.kt
Created May 18, 2018 08:34
CompletableFuture exception wrapping
import java.util.concurrent.*
fun main(args: Array<String>) {
class DomainSpecificException : RuntimeException()
val cf1 = CompletableFuture<Int>()
val cf2 = CompletableFuture<Int>()
val res = cf1.thenCompose { cf2 }
cf1.whenComplete { _, err -> println("cf1: Completed with error = $err") }
@elizarov
elizarov / DistinctInTimeWindow.kt
Created April 3, 2018 08:25
Channel operator to send distinct time elements in a specific time window
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.channels.*
import kotlinx.coroutines.experimental.selects.*
import java.util.concurrent.*
// operator
fun <T> ReceiveChannel<T>.distinctInTimeWindow(time: Long, unit: TimeUnit): ReceiveChannel<T> = produce {
require(time > 0)
consume {
val source = this@distinctInTimeWindow