Skip to content

Instantly share code, notes, and snippets.

@justinvdm
Last active December 30, 2017 15:01
Show Gist options
  • Save justinvdm/12482041a2e600f0fc65187300c66463 to your computer and use it in GitHub Desktop.
Save justinvdm/12482041a2e600f0fc65187300c66463 to your computer and use it in GitHub Desktop.
// @flow
type Msg<Type, V> = {|
type: Type,
val: V
|};
type AcceptFn =
& (<A, B, OV>('val', Msg<'val', A> => B) =>
(Msg<'val', A> | Msg<'err', OV>) => (B | Msg<'err', OV>))
& (<A, B, OV>('err', Msg<'err', A> => B) =>
(Msg<'err', A> | Msg<'val', OV>) => (B | Msg<'val', OV>));
declare var accept: AcceptFn;
function accept(type, fn) {
return msg => msg.type === type
? fn(msg)
: msg;
}
const pipe = <A, B, C>(fn1: A => B, fn2: B => C): (A => C) => (v: A) => fn2(fn1(v));
const createMsg = <Type, V>(type: Type, val: V): Msg<Type, V> => ({
type,
val
});
const fn1 = accept('val', (msg: Msg<'val', number>): Msg<'val', string> =>
createMsg('val', msg.val.toString()));
const fn2 = accept('err', (msg: Msg<'err', string>): Msg<'err', string> =>
createMsg('err', msg.val.toString()));
const fn3 = accept('val', (msg: Msg<'val', string>): Msg<'val', string> =>
createMsg('val', msg.val.toString()));
const fn4 = accept('err', (msg: Msg<'err', string>): Msg<'err', string> =>
createMsg('err', msg.val.toString()));
const fn = pipe(pipe(pipe(fn1, fn2), fn3), fn4);
fn(createMsg('val', 23));
[ignore]
[include]
[libs]
[lints]
[options]
[strict]
{
"name": "flow-multi-msg-pass",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"flow-bin": {
"version": "0.61.0",
"resolved": "https://registry.npmjs.org/flow-bin/-/flow-bin-0.61.0.tgz",
"integrity": "sha512-w6SGi5CDfKLNGzYssRhW6N37qKclDXijsxDQ5M8c3WbivRYta0Horv22bwakegfKBVDnyeS0lRW3OqBC74eq2g==",
"dev": true
}
}
}
{
"name": "flow-multi-msg-pass",
"version": "1.0.0",
"description": "",
"main": "-flow-msg-msg-pass.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"flow-bin": "^0.61.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment