Last active
October 30, 2017 16:44
-
-
Save Avaq/61ef751161365fe1a160b6034b33adfc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const after = t => x => cont => { | |
const id = setTimeout (cont, t, x) | |
return () => clearTimeout (id) | |
} | |
const map = f => run => cont => run(x => cont (f (x))) | |
const chain = f => run => cont => run(x => f (x) (cont)) | |
const run = chain (x => after (2000) (`${x}C`)) | |
(map (x => `${x}B`) | |
(after (1000) ('A'))) | |
const cancel = run (console.log) | |
// calling cancel will clear the timeout and allow the process to exit | |
// cancel() |
This file contains hidden or 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
const fs = require('fs') | |
const pipe = fs => fs.reduce ((f, g) => x => g (f (x)), x => x) | |
const readFile = p => rej => res => { | |
fs.readFile (p, 'utf8', (e, x) => e ? rej (e) : res (x)) | |
return () => {} | |
} | |
const after = t => x => _ => res => { | |
const id = setTimeout (res, t, x) | |
return () => clearTimeout (id) | |
} | |
const map = f => fork => rej => res => fork (rej) (x => res (f (x))) | |
const chain = f => fork => rej => res => fork (rej) (x => f (x) (rej) (res)) | |
const program = pipe ([ readFile | |
, map (x => x.trim ()) | |
, map (x => `${x}B`) | |
, chain (x => after (2000) (`${x}C`)) ]) | |
const fork = program ('test.txt') | |
const cancel = fork (e => console.error (`Handled ${e}`)) | |
(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment