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
/* | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the |
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 |
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.* | |
/** | |
* Defines deep recursive function that keeps its stack on the heap, | |
* which allows very deep recursive computations that do not use the actual call stack. | |
* To initiate a call to this deep recursive function use its [invoke] function. | |
* As a rule of thumb, it should be used if recursion goes deeper than a thousand calls. | |
* | |
* The [DeepRecursiveFunction] takes one parameter of type [T] and returns a result of type [R]. |
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
// UPDATE: Everyone finding this gist via Google! | |
// Modern kotlinx.coroutines has out-of-the-box support for asyncLazy with the following expression: | |
// val myLazyValue = async(start = CoroutineStart.LAZY) { ... } | |
// Use myLazyValue.await() when you need it | |
// ---------------- public api ---------------- | |
public interface AsyncLazy<out T> { | |
public suspend fun value(): T | |
public fun isInitialized(): Boolean |
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 Test { | |
int f(int x0, int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9) { | |
return (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) switch { | |
(>= 78, >= 80, >= 83, >= 62, < 90, >= 32, < 84, < 77, >= 51, >= 8) => 0, | |
(< 64, >= 85, >= 7, < 98, >= 71, < 26, >= 37, >= 34, < 5, < 39) => 1, | |
(>= 23, >= 56, >= 36, >= 64, < 27, < 72, >= 67, >= 28, >= 93, >= 73) => 2, | |
(>= 35, < 27, >= 71, >= 79, < 95, < 11, >= 86, < 32, < 32, >= 78) => 3, | |
(< 86, >= 83, < 90, < 99, < 35, < 6, < 69, < 80, < 4, < 85) => 4, | |
(< 38, >= 77, >= 70, >= 0, >= 9, < 96, < 5, < 39, >= 20, < 25) => 5, | |
(< 46, >= 71, >= 74, < 2, < 79, < 56, >= 34, < 8, < 53, >= 36) => 6, |
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
import kotlinx.coroutines.* | |
import kotlin.coroutines.* | |
/** | |
* Scope interface to control (pause & resume) timeout | |
*/ | |
interface TimerScope : CoroutineScope { | |
fun pause() | |
fun resume() | |
} |
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.experimental.* | |
import kotlin.coroutines.experimental.intrinsics.* | |
fun main(args: Array<String>) { | |
enumerate { | |
if (flip("A")) { | |
if (flip("B")) 1 else 2 | |
} else { | |
if (flip("C")) 3 else if (flip("D")) 4 else 5 | |
} |
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
// 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 { |
NewerOlder