Skip to content

Instantly share code, notes, and snippets.

View elizarov's full-sized avatar

Roman Elizarov elizarov

View GitHub Profile
// 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
@elizarov
elizarov / AwaitProblem.kt
Last active November 14, 2016 18:19
Demo the problem with naive await implementation
import java.util.concurrent.CompletableFuture
// ======================================================================================================
// demo the problem with naive await implementation
fun main(args: Array<String>) {
val n: Int = 100000
var actualCount = 0;
async<Unit> {
repeat(n) { // repeat n times
@elizarov
elizarov / Suspendable.kt
Last active November 11, 2016 16:09
Kotlin coroutines playground
import java.io.InputStream
import java.nio.ByteBuffer
import java.nio.channels.AsynchronousFileChannel
import java.nio.channels.CompletionHandler
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardOpenOption.CREATE
import java.nio.file.StandardOpenOption.WRITE
import java.time.Instant
import java.util.concurrent.CompletableFuture
/*
* QDS - Quick Data Signalling Library
* Copyright (C) 2002-2015 Devexperts LLC
*
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*/
package com.devexperts.util;
import java.util.BitSet;
/**
* @author Roman Elizarov
*/
public class Regex {
private final int n; // number of actual chars in pattern (w/o *)
private final char[] cs; // actual chars in pattens (w/o *)
private final boolean[] rs; // true when * applies
private final int[] fs; // transitive closure of match, given that an empty string matches x*