Created
September 23, 2019 21:07
-
-
Save saulshanabrook/4431323190f61b90fee6f55d90b86a7c to your computer and use it in GitHub Desktop.
This file contains 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
function handleNull<A, B, C, D>(middleware: Middleware<A, B, C, D>) | |
function applyMiddleware<A, B, C, D>( | |
middleware: Middleware<A, B, C, D>, | |
b_c: Fn<B, C> | |
): Fn<A, D | null> { | |
return a => { | |
const res = middleware(a); | |
if (!res) { | |
return null; | |
} | |
const [b, c_d] = res; | |
return c_d(b_c(b)); | |
}; | |
} | |
function composeMiddleware<A, B, C, D, E, F>( | |
l: Middleware<A, B, C, D>, | |
r: Middleware<B, E, F, C> | |
): Middleware<A, E, F, D> { | |
return (a) => { | |
const lRes = l(a) | |
if (!lRes) { | |
return null; | |
} | |
const [b, c_d] = lRes; | |
const rRes = r(b); | |
if (!rRes) { | |
return null; | |
} | |
const [e, f_c] = rRes; | |
return [e, f => c_d(f_c(f))] | |
} | |
} | |
type WithCost<T> = T & { cost: number }; | |
function incrementCostMiddleware<B, C>(): Middleware< | |
WithCost<B>, | |
B, | |
C, | |
WithCost<C> | |
> { | |
return ({ cost, ...rest }) => [rest as any, u => ({ cost: cost + 1, ...u })]; | |
} | |
function parseURL | |
const fn = applyMiddleware( | |
incrementCostMiddleware(), | |
({ a }: { a: number }) => ({ a, b: "hi" }) | |
); | |
const res = fn({ a: 10, cost: 50 }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment