Skip to content

Instantly share code, notes, and snippets.

View christianscott's full-sized avatar

Christian Scott christianscott

View GitHub Profile
import { Preconditions } from 'base/preconditions';
import { flushPromises } from 'base/testing/flush_promises';
import { runTasksWithRateLimit, Task } from '../run_tasks_with_rate_limit';
type Resolve<T> = (value?: T | PromiseLike<T>) => void;
type Reject = (reason: any) => void;
type Deferred<T = void> = Promise<T> & { resolve: Resolve<T>; reject: Reject };
const deferred = <T>(): Deferred<T> => {
import { DoublyLinkedList, ListNode, LRUCache } from '../lru_cache';
describe('DoublyLinkedList', () => {
test('keys', () => {
const dll = new DoublyLinkedList<string, number>();
const head: ListNode<string, number> = {
key: 'one',
value: 0,
prev: undefined,
@christianscott
christianscott / pick.ts
Created February 5, 2019 01:02
typesafe pick
function pick<T, K extends keyof T>(k: K): (t: T) => T[K] {
return (t: T) => t[k];
}
@christianscott
christianscott / garbage.ts
Created January 17, 2019 22:54
Proving a (terrible) point
type UnaryFunction<A, B> = (arg: A) => B
function pipe<T, A, B, C, D, E, F>(op1: UnaryFunction<T, A>, op2: UnaryFunction<A, B>, op3: UnaryFunction<B, C>, op4: UnaryFunction<C, D>, op5: UnaryFunction<D, E>, op6: UnaryFunction<E, F>): UnaryFunction<T, F>;
function pipe<T, R>(...operations: UnaryFunction<any, any>[]): UnaryFunction<T, R> {
return initial => operations.reduce((prev, nextFn) => nextFn(prev), initial)
}
const curry2 = <A, B, C>(fn: (a: A, b: B) => C) => (a: A) => (b: B) => fn(a, b)
const filter = curry2(<T>(fn: UnaryFunction<T, boolean>, arr: T[]) => arr.filter(fn))
import { Preconditions, UnreachableError } from 'base/preconditions';
export module FormFieldValue {
export const enum Type {
Text,
Select,
OnOff,
Numeric,
}
@christianscott
christianscott / maybe.ts
Created January 8, 2019 05:04
Typescript Maybe Monad (WIP)
export module Maybe {
enum Variant {
Just = 1,
Nothing,
}
export type Nothing = Readonly<{ type: Variant.Nothing }>;
export type Just<T> = Readonly<{ type: Variant.Just; value: T }>;
@christianscott
christianscott / fzf-checkout.fish
Created January 8, 2019 03:23
Fuzzy-find a branch and checkout to it
function fzf-checkout
git checkout (git branch | rg '^\s*[^*]' | fzf | sed 's/^ *//;s/ *$//')
end
const NO_ARGUMENTS_YET = Symbol("no arguments yet");
function allEqual<Arr extends any[]>(first: Arr, second: Arr): boolean {
return first.every((value, i) => value === second[i]);
}
export function memoizeOnce<ArgsT extends any[], ReturnT>(
fn: (...args: ArgsT) => ReturnT,
comparator: (prevArgs: ArgsT, nextArgs: ArgsT) => boolean = allEqual
) {
export function bufferStream(
handler: (events: Event[]) => void,
timeMs: number,
): (event: Event) => void {
let timeout: number = null;
let events: Event[] = [];
return (event: Event) => {
if (timeout != null) {
clearTimeout(timeout);
}
@christianscott
christianscott / optional-property.ts
Last active December 20, 2018 02:33
Make properties on an existing type optional
type OptionalProperty<T, P extends keyof T> = {
[K in Exclude<keyof T, P>]: T[K];
} & {
[K in Extract<keyof T, P>]?: T[K];
};
// Example
type Person = { age: number; name: string; country: string; };