Skip to content

Instantly share code, notes, and snippets.

View mekarthedev's full-sized avatar

Viacheslav Makarov mekarthedev

View GitHub Profile
@mekarthedev
mekarthedev / arrowloop-streamprocessor.hs
Created April 3, 2024 19:20
ArrowLoop instance for Hughes' StreamProcessor
import Control.Category
import Prelude hiding (id,(.))
import Control.Arrow
data StreamProcessor a b =
Put b (StreamProcessor a b) | Get (a -> StreamProcessor a b)
instance Category StreamProcessor where
id = arr id
@mekarthedev
mekarthedev / infer-ts-schema-from-json.ts
Created April 18, 2023 12:26
Hand made inference of a Typescript type from a custom json schema
type TypeFromString<T extends string> =
T extends "number" ? number
: T extends "string" ? string
: ["Invalid type string", T]
type InferPropRequired<PropSpec> = PropSpec extends { isRequired: false } ? false : true
type InferPropType<PropSpec> =
PropSpec extends { values: ReadonlyArray<infer Values> } ? Values
: PropSpec extends { type: infer T extends string } ? TypeFromString<T>
: ["Invalid prop spec", PropSpec]
@mekarthedev
mekarthedev / rxjava2-flatmapfirst.kt
Last active April 13, 2018 23:29
Effective flatMapFirst for RxJava2's Observable
import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
import java.util.concurrent.atomic.AtomicBoolean
fun <T, R> Observable<T>.flatMapFirst(transform: (T) -> Observable<R>) =
Observable.defer {
val busy = AtomicBoolean()
return@defer this
.filter { busy.compareAndSet(false, true) }
@mekarthedev
mekarthedev / fine-grained-variance-check.md
Last active September 9, 2023 21:51
Kotlin proposal: Allow `out` type parameter to occur in `in` position

Allow out type parameter to safely occur in in position

  • Type: Design proposal
  • Author: Viacheslav Makarov
  • Status: Draft

Summary

Reduce the need in @UnsafeVariance. Allow covariant types to have non-private methods with non-covariant signature. To protect class' covariance, out-project signatures