Skip to content

Instantly share code, notes, and snippets.

@wernerdegroot
wernerdegroot / concatMapAndBuffer.js
Created October 6, 2018 16:55
concatMapAndBuffer
const source = Rx.Observable.interval(1000).take(4)
const fetchData = i => Rx.Observable.timer(1500).map(() => i)
function concatMapAndBuffer(obs, fn) {
return Rx.Observable.create((observer) => {
let buffered = []
let completed = false
let alreadySubscribed = false
let innerSubscriber = new Rx.Subscriber()
import parsy
from collections import namedtuple
# Key:
letterParser = parsy.test_char(str.isalpha, 'letter')
keyParser = letterParser.at_least(1).concat()
# Separator between key and value:
spaceParser = parsy.string(' ')
equalsParser = parsy.string('=')
type LeftRight<Left, Right> = {
left: Left | false,
right: Right | false
}
type LeftsAndRightsWithSameId<Left, Right> = {
lefts: Left[],
rights: Right[]
}
@wernerdegroot
wernerdegroot / alternative.ts
Created June 8, 2018 08:06
Alternative to do notation
type Expression<Scope extends object> = keyof Scope | number
function constant<K extends string, Scope extends object, Value extends Expression<Scope>>(name: K, scope: Scope, value: Value): Scope & { [KK in K]: Value } {
return {
...(scope as any),
[name]: value
}
}
const newScope = constant('henk', {}, 4)
@wernerdegroot
wernerdegroot / Pipeline.ts
Last active May 25, 2018 06:41
Typescript pipelines
function range(from: number, toInclusive: number): number[] {
const result: number[] = []
for (let i = from; i <= toInclusive; ++i) {
result.push(i)
}
return result
}
function flatten<A>(as: A[][]): A[] {
return as.reduce((acc, curr) => [...acc, ...curr], [])
@wernerdegroot
wernerdegroot / Chapter 1.md
Last active May 15, 2018 18:19
Category theory

Implement, as best as you can, the identity function in your favorite language (or the second favorite, if your favorite language happens to be Haskell).

function identity<A>(a: A): A {
  return a
}

Implement the composition function in your favorite language. It takes two functions as arguments and returns a function that is their composition.

@wernerdegroot
wernerdegroot / strict.ts
Last active April 16, 2018 13:46
Strict null checks
type StrictUndefined = '3u328423u48234'
type StrictUndefinedMapping<O> =
true | Partial<{ [KK in keyof O]: StrictUndefinedMapping<O[KK]> }>
type Strict<O, M> = {
[KK in keyof O & keyof M]: M[KK] extends true ? (O[KK] | StrictUndefined) : Strict<O[KK], M[KK]>
} & {
[KK in Exclude<keyof O, keyof M>]: O[KK]
}
type Lazy<A> = () => A
function lazy<A>(fn: () => A): Lazy<A> {
type State
= { evaluated: false, evaluator: () => A }
| { evaluated: true, value: A }
let state: State = { evaluated: false, evaluator: fn }
return () => {
if (state.evaluated === true) {
return state.value
@wernerdegroot
wernerdegroot / p.ts
Created January 18, 2018 12:23
Typesafe paths
type Path1<O, K extends keyof O> = [K]
type Path2<O, K1 extends keyof O, K2 extends keyof O[K1]> = [K1, K2]
const x = {
hamster: true,
food: {
hamster: 'hamsterfood',
human: 4
}
}
@wernerdegroot
wernerdegroot / builder.ts
Last active January 8, 2018 19:09
Typesafe URL builder
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]
type Omit<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]}
const paramRegex = /:([^\/]+)/g
class UrlBuilder<Params> {
static fromUrl<Params>(url: string): UrlBuilder<Params> {
return new UrlBuilder<Params>(url, {})
}