Skip to content

Instantly share code, notes, and snippets.

View elizarov's full-sized avatar

Roman Elizarov elizarov

View GitHub Profile
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*
/*
* 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;
@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
@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
// 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
import javafx.beans.property.SimpleStringProperty
import javafx.geometry.Insets
import javafx.scene.Scene
import javafx.scene.control.ButtonBar
import javafx.scene.control.ButtonType
import javafx.scene.control.Dialog
import javafx.util.Callback
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.javafx.JavaFx
import kotlinx.coroutines.experimental.launch
@elizarov
elizarov / CancellableHandlerContext.kt
Created April 24, 2017 08:15
Alternative implementation of HandlerContext for Android that continues coroutine with CancellationException when the job is complete (i.e. it was cancelled from outside)
public class CancellableHandlerContext(
private val handler: Handler
) : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor, Delay
{
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> =
DispatchedContinuation(continuation)
override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) {
handler.postDelayed(object : ResumeTask<Unit>(continuation) {
override fun resume() = continuation.resume(Unit)
@elizarov
elizarov / MultiShotEnumeration.kt
Created May 3, 2017 09:01
Showcase for Kotlin multishot continuations
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
}