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.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") | |
} |
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 <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? |
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
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 |
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 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() } |
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
// 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 { |
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 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: | |
* | |
* ``` |
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
package a; | |
public abstract class A { | |
abstract void foo(); | |
public void callFooA() { | |
foo(); | |
} | |
} |
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
package a; | |
public abstract class A { | |
abstract void foo(); | |
public void callFooA() { | |
System.out.print("callFooA: "); | |
foo(); | |
} | |
} |
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
/* | |
* 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()) |
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 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). |