Skip to content

Instantly share code, notes, and snippets.

View elizarov's full-sized avatar

Roman Elizarov elizarov

View GitHub Profile
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")
}
@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?
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
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() }
@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 {
@elizarov
elizarov / Delimited.kt
Last active October 23, 2023 20:19
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 / 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 / A.java
Created January 30, 2019 14:56
Больше ада
package a;
public abstract class A {
abstract void foo();
public void callFooA() {
System.out.print("callFooA: ");
foo();
}
}
@elizarov
elizarov / AD.kt
Last active February 26, 2023 15:18
Automatic Differentiation with Kotlin
/*
* Implementation of backward-mode automatic differentiation.
*/
/**
* Differentiable variable with value and derivative of differentiation ([grad]) result
* with respect to this variable.
*/
data class D(var x: Double, var d: Double = 0.0) {
constructor(x: Int): this(x.toDouble())
@elizarov
elizarov / Rec.kt
Last active June 1, 2019 00:16
Recursive computation engine for Kotlin
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
/**
* Recursive computation engine for Kotlin that uses suspending functions to keep computation
* stack on the heap (as opposed to the real stack).
*
* This implementation is hardcoded for functions with two parameters [V1] and [V2] that
* return [R] as result, but it can be easily modified for functions with different
* number of parameters (see code).