Skip to content

Instantly share code, notes, and snippets.

Roman Elizarov elizarov

Block or report user

Report or block elizarov

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@elizarov
elizarov / PauseableTimeout.kt
Created Nov 8, 2019
A version of withTimeoutOrNull that can be paused/resumed.
View PauseableTimeout.kt
import kotlinx.coroutines.*
import kotlin.coroutines.*
/**
* Scope interface to control (pause & resume) timeout
*/
interface TimerScope : CoroutineScope {
fun pause()
fun resume()
}
View gist:b24935058a13fb8d1755816bdb4beecd
https://drive.google.com/open?id=10_FO40m5KU4mW59vzS3KlL2Kb7zUWvEc
@elizarov
elizarov / Wrapper.kt
Created Jun 5, 2019
Simple wrapper function combinator engine for Kotlin
View Wrapper.kt
// ============ test functions ============
fun withTransactional() = transactional {
println("withTransactional body")
}
fun withLogged() = logged {
println("withLogged body")
}
@elizarov
elizarov / RecursiveFunction.kt
Last active Nov 23, 2019
Defines recursive function that keeps its stack on the heap (productized version)
View RecursiveFunction.kt
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
import kotlin.math.*
/**
* 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.
*
* The function takes one parameter of type [T] and returns a result of type [R].
* The [block] of code defines the body of a recursive function. In this block
@elizarov
elizarov / Rec.kt
Last active Jun 1, 2019
Recursive computation engine for Kotlin
View Rec.kt
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).
@elizarov
elizarov / AD.kt
Last active Aug 6, 2019
Automatic Differentiation with Kotlin
View AD.kt
/*
* 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 / A.java
Created Jan 30, 2019
Больше ада
View A.java
package a;
public abstract class A {
abstract void foo();
public void callFooA() {
System.out.print("callFooA: ");
foo();
}
}
@elizarov
elizarov / A.java
Created Jan 30, 2019
Package-private inheritance
View A.java
package a;
public abstract class A {
abstract void foo();
public void callFooA() {
foo();
}
}
@elizarov
elizarov / Delimited.kt
Last active Nov 4, 2019
Delimited Continuations shift/reset in Kotlin
View Delimited.kt
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 Nov 29, 2019
Aspect operators
View AspectOperators.kt
// 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 {
You can’t perform that action at this time.