Skip to content

Instantly share code, notes, and snippets.

View elizarov's full-sized avatar

Roman Elizarov elizarov

View GitHub Profile
@elizarov
elizarov / StateFlowDesign.md
Last active December 26, 2019 15:18
StateFlow Design Draft

StateFlow Design Draft

UI applications often need to maintain observable state. The state is conceptually a time-changing variable that always has a value that can be observed by multiple views. Currently, ConflatedBroadcastChannel is used for such state representation but it is not very efficient, because it has to provide full-blown implementation of SendChannel for updating state and ReceiveChannel for observing state, but far simpler concept is actually needed in practice.

Proposal

@elizarov
elizarov / FlowWithState.md
Last active February 5, 2020 20:27
Flow.withState Design Draft

Flow.withState Design Draft

There's a whole set of potential Flow operators that maintain some kind of state for each active collection. For example, Flow.withIndex and conceptually similar operators.

Proposal

Introduce withState operator to simplify implementation for all kinds of stateful operators:

@elizarov
elizarov / FlowWithState.md
Created December 26, 2019 10:00
Flow.withState Design

Introduction

There's a whole set of potential Flow operators that maintain some kind of state for each active collection. For example, Flow.withIndex and conceptually similar operators.

Proposal

The proposal is to introduce a generic withState operator to simplify implementation for all of them:

@elizarov
elizarov / PauseableTimeout.kt
Created November 8, 2019 08:41
A version of withTimeoutOrNull that can be paused/resumed.
import kotlinx.coroutines.*
import kotlin.coroutines.*
/**
* Scope interface to control (pause & resume) timeout
*/
interface TimerScope : CoroutineScope {
fun pause()
fun resume()
}
https://drive.google.com/open?id=10_FO40m5KU4mW59vzS3KlL2Kb7zUWvEc
@elizarov
elizarov / Wrapper.kt
Created June 5, 2019 21:58
Simple wrapper function combinator engine for Kotlin
// ============ test functions ============
fun withTransactional() = transactional {
println("withTransactional body")
}
fun withLogged() = logged {
println("withLogged body")
}
@elizarov
elizarov / DeepRecursiveFunction.kt
Last active March 25, 2024 00:40
Defines recursive function that keeps its stack on the heap (productized version)
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].
@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).
@elizarov
elizarov / AD.kt
Last active September 5, 2024 00:32
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 / 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();
}
}