Skip to content

Instantly share code, notes, and snippets.

View Willmo36's full-sized avatar

Max Willmott Willmo36

  • SkipTheDishes
  • Saskatoon
View GitHub Profile
@Willmo36
Willmo36 / MapTo.ts
Created June 25, 2019 15:09
TypeScript MapTo mapped type
export type MapTo<T extends Record<string, V>, K extends keyof V, V> = {
[K2 in keyof T]: T[K2][K]
};
@Willmo36
Willmo36 / RecordUnion.ts
Created June 24, 2019 18:22
Turn a record into a union of {key,value} objects
// Convert record to union of its members
// eg {foo: string, bar: number} -> {key: 'foo', value: string} | {key: 'bar' value: number};
type RecordUnion<T extends Record<string, any>> = {
[K in keyof T]: { key: K, value: T[K] }
}[keyof T];
@Willmo36
Willmo36 / UndefinedToUnrequired.ts
Last active June 19, 2019 20:01
Replace required `| undefined` keys with unrequired keys
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
type OptionalKeys<T> = Exclude<{
[K in keyof T]: Extract<T[K], undefined> extends never ? never : K;
}[keyof T], undefined>;
type UndefinedToUnrequired<T> = {
[K in OptionalKeys<T>]?: T[K]
} & Omit<T, OptionalKeys<T>>
@Willmo36
Willmo36 / zio.ts
Created May 31, 2019 17:09
First attempt at zio inspiried monad in TypeScript
import { Either, left, right } from "fp-ts/lib/Either";
import { Task, task } from "fp-ts/lib/Task";
type Env<R, E, A> = (r: R) => Task<Either<E, A>>;
const run = <R, E, A>(r: R, env: Env<R, E, A>) => env(r);
const of = <R, E, A>(a: A): Env<R, E, A> =>
(r: R) => task.of(right(a));
@Willmo36
Willmo36 / withCollisionDect.ts
Created March 15, 2019 16:48
Basic trigonometry via tiny game - Collision detection
type Pair = [number, number];
type Entity = {
dimensions: Pair;
pos: Pair;
img: HTMLImageElement;
collider: number;
};
type GameState = {
keyCode: number | null;
player: Entity;
@Willmo36
Willmo36 / index.ts
Last active March 14, 2019 14:46
Basic trigonometry via tiny game
type Pair = [number, number];
type GameState = {
keyCode: number | null;
playerPos: Pair;
playerImg: HTMLImageElement;
enemyPos: Pair;
enemyImg: HTMLImageElement;
};
function game() {
@Willmo36
Willmo36 / BezierCurve.purs
Last active January 28, 2019 22:37
PureScript Bezier Curve experiments
module Main where
import Prelude
import Effect (Effect)
import Effect.Console (log)
import Debug.Trace (trace)
import Math (pow, pi)
import Graphics.Canvas
import Partial.Unsafe (unsafePartial)
import Data.Maybe (Maybe(..))
@Willmo36
Willmo36 / FingerTree.ts
Last active December 7, 2018 12:07
Implementing a FingerTree in TypeScript with fp-ts
/* References
https://apfelmus.nfshost.com/articles/monoid-fingertree.html
https://abhiroop.github.io/Finger-Trees/
https://chrispenner.ca/posts/intro-to-finger-trees
http://www.staff.city.ac.uk/~ross/papers/FingerTree.html
*/
import { Monoid } from "fp-ts/lib/Monoid";
import { Newtype, iso } from "newtype-ts";
import { Predicate } from "fp-ts/lib/function";
@Willmo36
Willmo36 / SKI.ts
Created December 4, 2018 11:04
SKI combinators in TypeScript
type K = <A>(a: A) => <B>(b: B) => A;
type S = <A, B, C>(abc: (a: A) => (b: B) => C) => (ab: (a: A) => B) => (a: A) => C;
const K: K = a => b => a;
const S: S = abc => ab => a => abc(a)(ab(a));
type IGNORED = "ignored";
function IforallT<T>(t: T): (t: T) => T {
//To make I from S we need to achieve
//a :: T, b is ignored, c == a :: T
//if b is ignored then the ab arg of S becomes irrelevant,
@Willmo36
Willmo36 / queue-pf.ts
Created December 3, 2018 18:27
TypeScript (almost) point free Queue
import { head as headArr, reverse, snoc as snocArr, tail as tailArr } from "fp-ts/lib/Array";
import {
applyFlipped,
compose,
constant,
Curried2,
Curried3,
curry,
Endomorphism,
flip,