const add = (a, b) => a + b;
const add : (a: number, b: number) => number = (a, b) => a + b;
const add = (a, b, cb) => cb(a + b);
const add : (a: number, b: number, cb: (c: number) => void) => void = (a, b, cb) => cb(a + b);
continuation passing style
const add : (a: number) => (b: number) => (cb: (c: number) => void) => void = a => b => cb => cb(a + b);
Cont<number>
type Cont<T> = (cb: (x: T) => void) => void; const add : (a: number) => (b: number) => Cont<number> = a => b => cb => cb(a + b);
// XXX: type error
const result = add(add(3, 4), add(5, 6));
我要怎麼 add
其他 add
的結果?
Cont<number>
sconst pure : <T>(x: T) => Cont<T> = x => cb => cb(x); const add : (a: Cont<number>) => (b: Cont<number>) => Cont<number> = a => b => cb => a(x => b(y => cb(x + y))); const result = add( add(pure(3), pure(4)), add(pure(5), pure(6)), );
Promise<number>
sconst add : (a: Promise<number>, b: Promise<number>) => Promise<number> = (a, b) => a.then(x => b.then(y => x + y));
Anime<T>
type Anime<T> = (time: number) => T; const three: Anime<number> = (time) => 3; const twice: Anime<number> = (time) => 2 * time; const add : (ax: Anime<number>) => (ay: Anime<number>) => Anime<number> = ax => ay => time => ax(time) + ay(time);
Promise
const a = Promise.resolve(3); const b = Promise.resolve(4); const c = a.then(x => b.then(y => x + y));
const a = Promise.resolve(3); const b = Promise.resolve(4); const c = await a + await b;
const [a = 0] = usePromise(Promise.resolve(3)); const [b = 0] = usePromise(Promise.resolve(4)); const c = a + b;
type ThunkAction<Args extends any[], U> = (d: Dispatch, s: GetState) => (...args: Args) => Promise<U>
type Saga<Args extends any[] = any[]> = (...args: Args) => IterableIterator<any>
interface Epic< Input extends Action = any, Output extends Input = Input, State = any, Dependencies = any > { ( action$: Observable<Input>, state$: StateObservable<State>, dependencies: Dependencies ): Observable<Output>; }
上述技術都過時了 :(