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 / traversal.ts
Created October 15, 2020 16:02
monocle-ts traversal example
import * as T from "monocle-ts/lib/Traversal";
import * as R from "fp-ts/lib/Record";
import { pipe } from "fp-ts/lib/function";
import { Lens } from "monocle-ts";
type Person = {
age: number;
hairColor: string;
};
@Willmo36
Willmo36 / union_to_intersection.ts
Created August 4, 2020 15:44
TypeScript union to intersection from FP slack
//Source: https://functionalprogramming.slack.com/archives/C7BBY9A95/p1594724501330700?thread_ts=1594687497.326100&cid=C7BBY9A95
type UnionToIntersection<U> =
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
@Willmo36
Willmo36 / ref.option.tsx
Created April 9, 2020 20:31
fp-ts React ref proxy
function refProxy<T>(
ref: React.MutableRefObject<T>
): React.MutableRefObject<T> & { current: O.Option<T> } {
const p = new Proxy(
ref as React.MutableRefObject<T> & { current: O.Option<T> },
{
get: (target, prop) => {
if (prop === "current") {
return O.fromNullable(target.current);
@Willmo36
Willmo36 / synax.ts
Created March 30, 2020 15:03
New TS syntax to me
interface CurriedFn2<A, B, R> {
<A>(a: A): CurriedFn1<B, R>
<A, B>(a: A, b: B): R
}
@Willmo36
Willmo36 / HKD.ts
Created March 12, 2020 17:50
Higher Kinded Data fp-ts
import {Applicative1} from 'fp-ts/lib/Applicative';
import * as Array from 'fp-ts/lib/Array';
import {Kind, URIS} from 'fp-ts/lib/HKT';
import {pipe} from 'fp-ts/lib/pipeable';
export type HKD<F extends URIS, A> = {[K in keyof A]: Kind<F, A[K]>};
export const sequenceHKD = <F extends URIS>(F: Applicative1<F>) => <A>(
lifted: HKD<F, A>,
): Kind<F, A> => {
@Willmo36
Willmo36 / fp-ts.composedPath.ts
Created March 11, 2020 15:55
composedPath fallback via unfolding
import { array } from 'fp-ts/lib/Array';
import { flow, tuple } from 'fp-ts/lib/function';
import * as Option from 'fp-ts/lib/Option';
export const composedPath = (e: MouseEvent) => {
if (e['composedPath']) {
return e.composedPath();
}
if (e.target instanceof HTMLElement) {
@Willmo36
Willmo36 / fp-ts.foldMap.ts
Created March 10, 2020 21:17
fp-ts FoldMap example
import * as A from "fp-ts/lib/Array";
import * as M from "fp-ts/lib/Map";
import { eqNumber } from "fp-ts/lib/Eq";
//My domain type
type User = {
name: string;
age: number;
};
@Willmo36
Willmo36 / getMagmaRight.ts
Created February 13, 2020 15:29
fp-ts magma for discarding left value (think Object.assign)
export const getMagmaRight = <A>(): Magma<A> => ({
concat: (a, b) => b
});
@Willmo36
Willmo36 / assertSome.ts
Created February 13, 2020 15:23
fp-ts assert Option.Some
const assertOption = <A>(op: Option.Option<A>): A => {
if(Option.isNone(op)) {
throw new Error("assertSome failed.")
}
return op.value;
}
@Willmo36
Willmo36 / MapInvert.ts
Created February 13, 2020 15:18
fp-ts map map invert
export const invert = <A,B,C>(ordA: Ord<A>, ordB:Ord<B>) => (map:Map<A,Map<B,C>>): Map<B,Map<A,C>> => {
const arrayABC = pipe(
map,
M.toArray(ordA),
Array.map(([a, mapBC]) => pipe(
mapBC,
M.toArray(ordB),
Array.map(([b,c]) => [a,b,c] as const)
)),
Array.flatten