Skip to content

Instantly share code, notes, and snippets.

object Sentries {
private val callbacks = ConcurrentHashMap<Reference<*>, () -> Unit>()
private val refs = HashSet<WeakReference<*>>()
private val refQueue = ReferenceQueue<Sentry<*>>()
private val poller =
Observable.interval(10000, TimeUnit.MILLISECONDS)
.subscribe {
refQueue.poll()?.let { ref ->
callbacks.remove(ref)?.invoke()
@happy-bracket
happy-bracket / Singles
Created June 17, 2019 08:29
For medium 1
Single.just(3).map { x -> x.toString() }
class SetBackedMap<K, V>(private val set: Set<Map.Entry<K, V>>) : Map<K, V> {
override val entries: Set<Map.Entry<K, V>> = set
override val keys: Set<K> = set.map { (key, _) -> key }.toSet()
override val size: Int = set.size
override val values: Collection<V> = set.map { (_, value) -> value }
override fun containsKey(key: K): Boolean {
return set.any { (possibleKey, _) -> possibleKey == key }
}
sealed class DiffOp {
data class Insert(val oldPos: Int, val newPos: Int) : DiffOp()
data class Delete(val oldPos: Int) : DiffOp()
}
fun <T> diff(list1: MutableList<T>, list2: MutableList<T>, i: Int = 0, j: Int = 0): List<DiffOp> {
val N = list1.size
@happy-bracket
happy-bracket / nc.kt
Created July 11, 2019 12:16
Nullable Comprehension
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
class NullableComprehension<R> : Continuation<R> {
override val context: CoroutineContext = EmptyCoroutineContext
private var internalResult: R? = null
@happy-bracket
happy-bracket / JEither.java
Created July 29, 2019 08:17
Java Either, woo hoo
class JEither<L, R> {
private L left;
private R right;
private JEither() {}
public <C> C fold(Function<L, C> ifLeft, Function<R, C> ifRight) {
if (left != null) {
return ifLeft.apply(left);
class BetterWhen<T, R>(val appl: T) {
var result: R? = null
fun cond(p: (T) -> Boolean, res: () -> R) {
if (p(appl)) {
result = res()
throw Interrupt()
Kto prochital, tot gey))0)
import io.reactivex.Single
import io.reactivex.functions.BiFunction
interface Kind<out C, out A>
class ForSingle private constructor()
class SingleContainer<T>(val single: Single<T>): Kind<ForSingle, T>
fun <T> Kind<ForSingle, T>.fix() = (this as SingleContainer<T>).single
fun <T> Single<T>.unfix() = SingleContainer(this)
@happy-bracket
happy-bracket / either.dart
Created September 21, 2019 13:16
Either, but in Dart
typedef Function1<A, R> = R Function(A param);
class Either<L, R> {
bool _isRight;
L _left;
R _right;
C fold<C>(Function1<L, C> ifLeft, Function1<R, C> ifRight) {
if (_isRight) {