Skip to content

Instantly share code, notes, and snippets.

@saulshanabrook
Created September 23, 2019 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saulshanabrook/4431323190f61b90fee6f55d90b86a7c to your computer and use it in GitHub Desktop.
Save saulshanabrook/4431323190f61b90fee6f55d90b86a7c to your computer and use it in GitHub Desktop.
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