Skip to content

Instantly share code, notes, and snippets.

View KEIII's full-sized avatar
😎
show me the code

Ivan Kasenkov KEIII

😎
show me the code
View GitHub Profile
// [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]];
}
// 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
@KEIII
KEIII / flatIntoTree.ts
Created February 17, 2021 09:15
Converts flat list into tree
type FlatItem<U> = {
uuid: U;
parent: U | null;
};
export type TreeBranch<T> = Omit<T, 'parent'> & {
parent: TreeBranch<T> | null;
children: TreeBranch<T>[];
};
@KEIII
KEIII / observervable.ts
Created January 28, 2021 14:34
simple observable pattern
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 => {
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 }
: {};
@KEIII
KEIII / example.test.ts
Last active January 16, 2021 17:06
Maybe (Functor, Applicative & Monad) for TypeScript
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);
@KEIII
KEIII / monad.ts
Last active April 19, 2023 16:31
Functor & Monad type
/**
* 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
@KEIII
KEIII / chain.ts
Last active January 16, 2021 19:37
Composite functions for TypeScript
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
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;
@KEIII
KEIII / ts-extra-types.ts
Created August 24, 2020 18:08
TypeScript extra types
export type RecursivePartial<T> = {
[P in keyof T]?: T[P] extends (infer U)[]
? RecursivePartial<U>[]
: T[P] extends object
? RecursivePartial<T[P]>
: T[P];
};