Skip to content

Instantly share code, notes, and snippets.

@Avaq
Last active October 30, 2017 16:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Avaq/61ef751161365fe1a160b6034b33adfc to your computer and use it in GitHub Desktop.
Save Avaq/61ef751161365fe1a160b6034b33adfc to your computer and use it in GitHub Desktop.
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()
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