This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [1, 2, 3] -> [ [ 1, 2 ], [ 2, 3 ], [ 3, 1 ] ] | |
const intoChainedSegments = points => points.reduce((result, n) => { | |
if (result.length === 0) { | |
return [[n]]; | |
} else { | |
const [[a,]] = result; | |
const [[x,]] = result.slice(-1); | |
const heads = result.slice(0, result.length - 1); | |
return [...heads, [x, n], [n, a]]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// const :: a -> b -> a | |
const Const = a => _ => a | |
// Just :: a -> Maybe a | |
const Just = a => ({ _tag: 'Just', a }); | |
// Nothing :: Maybe a | |
const Nothing = { _tag: 'Nothing' }; | |
// (.) :: (b -> c) -> (a -> b) -> a -> c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type FlatItem<U> = { | |
uuid: U; | |
parent: U | null; | |
}; | |
export type TreeBranch<T> = Omit<T, 'parent'> & { | |
parent: TreeBranch<T> | null; | |
children: TreeBranch<T>[]; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Observer<T> = (value: T) => void; | |
export const observable = <T>() => { | |
const observers: Observer<T>[] = []; | |
return { | |
subscribe: (observer: Observer<T>) => { | |
observers.push(observer); | |
}, | |
next: (value: T) => { | |
observers.forEach(observer => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ParamValue = string | number; | |
type ExtractRouteParams<T> = string extends T | |
? Record<ParamValue, ParamValue> | |
: T extends `${infer _Start}:${infer Param}/${infer Rest}` | |
? { [k in Param | keyof ExtractRouteParams<Rest>]: ParamValue } | |
: T extends `${infer _Start}:${infer Param}` | |
? { [k in Param]: ParamValue } | |
: {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Maybe, Nothing, Just } from "./maybe"; | |
test("composition", () => { | |
const f = (ma: Maybe<number>) => { | |
return ma | |
.flatMap((x) => (x % 2 === 0 ? Nothing : Just(x + 1))) | |
.map((x) => x * x); | |
}; | |
expect(f(Nothing)).toEqual(Nothing); | |
expect(f(Just(2))).toEqual(Nothing); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A functor is simply something that can be mapped over | |
*/ | |
type Functor<A> = { | |
unit: (x: A) => Functor<A>; | |
map: <B>(f: (x: A) => B) => Functor<B>; // aka fmap | |
} | |
/** | |
* For a monad `M`, a value of type M<A> represents having access |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type F<A, B> = (x: A) => B; | |
const chain = <A, B>(fab: F<A, B>) => { | |
return { | |
chain: <C>(fbc: F<B, C>) => chain((x: A) => fbc(fab(x))), | |
value: fab, | |
}; | |
}; | |
// example |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { of, timer, Subject } from 'rxjs'; | |
import { catchError, map, switchMap } from 'rxjs/operators'; | |
import { withLoading } from './withLoading'; | |
const request = new Subject<number>(); | |
const fakeResponse = (n: number) => { | |
return timer(1_000).pipe( | |
map(() => { | |
if (Math.random() > 0.5) throw 'Shit happens'; | |
return n * n; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type RecursivePartial<T> = { | |
[P in keyof T]?: T[P] extends (infer U)[] | |
? RecursivePartial<U>[] | |
: T[P] extends object | |
? RecursivePartial<T[P]> | |
: T[P]; | |
}; | |