Skip to content

Instantly share code, notes, and snippets.

View Lucifier129's full-sized avatar
🎯
Focusing

工业聚 Lucifier129

🎯
Focusing
View GitHub Profile
@Lucifier129
Lucifier129 / merge-type.ts
Created September 5, 2021 11:56
conditional union/intersection types
// type utils
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (
x: infer R
) => any
? R
: never;
type UnionOf<T extends object> = T[keyof T];
type IntersectionOf<T extends object> = UnionToIntersection<UnionOf<T>>;
type ProcedureContext = {
path: Procedure[];
next: (procedure: Procedure) => void;
};
type Procedure = {
run: (ctx: ProcedureContext) => unknown;
};
const runProcedure = (
@Lucifier129
Lucifier129 / algebraic-effects.js
Created June 28, 2018 14:33
algebraic effects and handlers for promise and observable
import { interval, isObservable } from 'rxjs'
import { tap } from 'rxjs/operators'
const type = {
isThenable: obj => !!(obj && typeof obj.then === 'function'),
isObservable: obj => !!(obj && isObservable(obj)),
isError: obj => obj instanceof Error,
isObject: obj => obj != null && typeof obj === 'object'
}
@Lucifier129
Lucifier129 / recoil-via-build-system.ts
Created May 13, 2021 03:37
implement recoil-like api via build-system abstraction
export type Fetch<K = unknown, V = unknown> = (key: K) => V
type Task<K = unknown, V = unknown> = (fetch: Fetch<K, V>) => V
type Tasks<K = unknown, V = unknown> = (key: K) => Task<K, V> | null
type Store<K, V> = Map<K, V>
type Build<K, V> = (tasks: Tasks<K, V>, key: K, store: Store<K, V>) => void
type PassengerEnName = {
type: 'PassengerEnName';
firstname: string;
middlename?: string;
lastname: string;
};
type PassengerCnName = {
type: 'PassengerCnName';
firstname: string;
type ResourcesValue<T> = T extends []
? never
: T extends [Resource<infer V>]
? [V]
: T extends [Resource<infer V>, ...infer Rest]
? [V, ...ResourcesValue<Rest>]
: never;
type InitResource = {
init<T>(
@Lucifier129
Lucifier129 / naive-json-parser.js
Last active December 23, 2020 23:15
a naive json parser implemented by parser combinator
// parser combinator
const fail = () => []
const failed = list => list.length === 0
const of = value => input => [value, input]
const bind = (parser, f) => input => {
let result = parser(input)
type ParseResult = {
success: boolean,
data: any,
source: string
}
type ParseFailed = {
suceess: false,
data: any,
source: string
const log = (message) => {
return {
__typename: 'log',
message,
};
};
const raise = (message) => {
return {
__typename: 'exception',
function pipe<T, A>(a: T[], a1: ListAction<T, A>): A[];
function pipe<T, A, B>(a: T[], a1: ListAction<T, A>, a2: ListAction<A, B>): B[];
function pipe<T, A, B, C>(
a: T[],
a1: ListAction<T, A>,
a2: ListAction<A, B>,
a3: ListAction<B, C>
): C[];