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 / StateBuilder.ts
Last active July 3, 2020 15:35
fp-ts comonad builder
type MyState = {
store1: {foo: number},
store2: {bar: number}
};
const initialState: MyState = {
store1: {foo: 0},
store2: {bar: 0},
}
@Willmo36
Willmo36 / priority.fp-ts.ts
Created January 22, 2020 15:38
FP-TS Priority type
import * as Ord from 'fp-ts/lib/Ord';
import * as Eq from 'fp-ts/lib/Eq';
import {pipe} from 'fp-ts/lib/pipeable';
import * as Array from 'fp-ts/lib/Array';
import * as NonEmptyArray from 'fp-ts/lib/NonEmptyArray';
export type Priority<A> = {
priority: number;
value: A;
};
@Willmo36
Willmo36 / monocle.nested.ts
Created October 24, 2019 18:23
Monocle-ts nested ?
import { Lens } from 'monocle-ts'
type Foo = {
bar?: {
baz?: number[]
}
}
const bar = Lens.fromNullableProp<Foo>()('bar', {})
const baz = Lens.fromNullableProp<Foo['bar']>()("baz", [])
@Willmo36
Willmo36 / monocle.nested.ts
Created October 24, 2019 18:22
Monocle-ts Nested options
import { Optional } from "monocle-ts";
import * as Option from "fp-ts/lib/Option";
type Baz2 = number;
type Bar2 = {
baz: Option.Option<Baz2>;
};
type Foo2 = {
bar: Option.Option<Bar2>;
};
@Willmo36
Willmo36 / restricted.tsx
Created October 23, 2019 17:17
Restricted Monad experiment
import React from "react"
type Level = "read" | "read_write"
type Restricted<A, B> = (l: Level) => (a: A) => B
function restricted<A, B>(required: Level, whenAuth: (a: A) => B, whenNotAuth: () => B) {
return (level: Level) => (a: A) => required === level
? whenAuth(a)
: whenNotAuth();
}
@Willmo36
Willmo36 / restricted.ts
Last active October 24, 2019 15:28
Restricted Monad experiment
import * as Option from "fp-ts/lib/Option";
import { pipe } from "fp-ts/lib/pipeable";
type Permission = "read" | "read_write";
interface Restricted<A, B> {
(l: Permission): (a: A) => Option.Option<B>;
}
//so instead of having the whenNotAuthed, that only is given upon "run" along with level
@Willmo36
Willmo36 / liftA2.fp-ts.ts
Last active October 18, 2019 18:08
liftA2 fp-ts + Option example
import * as A from "fp-ts/lib/Applicative";
import { URIS, Kind } from "fp-ts/lib/HKT";
import { Option, option, some } from "fp-ts/lib/Option";
const lift = <F extends URIS, A, B, C>(
F: A.Applicative1<F>,
fn: (a: A) => (b: B) => C
) => (fa: Kind<F, A>) => (fb: Kind<F, B>): Kind<F, C> => {
const fab = F.map(fa, fn);
const c = F.ap(fab, fb);
@Willmo36
Willmo36 / max-comonad.hs
Created October 6, 2019 00:52
Max learning various comonads
module Main where
import Control.Comonad
data Stream a = a :> Stream a
instance Functor Stream where
fmap f (a :> rest) = f a :> (fmap f rest)
@Willmo36
Willmo36 / flip.uncurry.ts
Created August 27, 2019 21:49
TypeScript flip n uncurry
function flip<Args1 extends any[], Args2 extends any[], R>(
fn: (...args1: Args1) => (...args2: Args2) => R
) {
return (...args2: Args2) => (...args1: Args1) => fn(...args1)(...args2);
}
function uncurry<A, Args2 extends any[], R>(
fn: (arg: A) => (...args2: Args2) => R
) {
ghcid "--command=stack ghci --ghci-options -i src/Main.hs"
//https://github.com/dramforever/vscode-ghc-simple