-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use Fluture #36
base: main
Are you sure you want to change the base?
use Fluture #36
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved, with some optional code style suggestions.
// writeStdOut :: String -> Future Error Undefined | ||
const writeStdOut = data => ( | ||
Future.node (callback => process.stdout.write (data, 'utf8', callback)) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, I've known for a while that process.stdout.write
is non-blocking, but I never knew it takes a callback. :)
// parseFile :: Options -> String -> Future Error String | ||
const parseFile = options => filename => ( | ||
map (promap (lines) | ||
(unlines) | ||
(B (snd) | ||
(reduce (flip (line => | ||
pair (num => B (Pair (num + 1)) | ||
(append (parseLine (options) | ||
(filename) | ||
(num) | ||
(line)))))) | ||
(Pair (1) ([]))))) | ||
(readFile (filename)) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably unnest some of this by extracting pieces:
// parseFile :: Options -> String -> Future Error String | |
const parseFile = options => filename => ( | |
map (promap (lines) | |
(unlines) | |
(B (snd) | |
(reduce (flip (line => | |
pair (num => B (Pair (num + 1)) | |
(append (parseLine (options) | |
(filename) | |
(num) | |
(line)))))) | |
(Pair (1) ([]))))) | |
(readFile (filename)) | |
); | |
// overLines :: (Array String -> Array String) -> String -> String | |
const overLines = promap (lines) (unlines); | |
// parseFile :: Options -> String -> Future Error String | |
const parseFile = options => filename => { | |
const parse = parseLine = (options) (filename); | |
const reducer = line => pair (num => B (Pair (num + 1)) | |
(append (parse (num) (line)))); | |
const parseLines = B (snd) (reduce (flip (reducer)) (Pair (1) ([]))); | |
return T (readFile (filename)) (map (overLines (parseLines))); | |
}; |
(chain (output => | ||
program.insertInto == null ? | ||
writeStdOut (output) : | ||
chain (writeFile (program.insertInto)) | ||
(map (replace (delimiters) ('$1\n\n' + output + '\n$2')) | ||
(readFile (program.insertInto)))) | ||
(transcribe (options) (program.args))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use thrush as a poor-man's pipe operator to make the data flow and indentation a bit more natural.
(chain (output => | |
program.insertInto == null ? | |
writeStdOut (output) : | |
chain (writeFile (program.insertInto)) | |
(map (replace (delimiters) ('$1\n\n' + output + '\n$2')) | |
(readFile (program.insertInto)))) | |
(transcribe (options) (program.args))); | |
(T (transcribe (options) (program.args)) | |
(chain (output => | |
program.insertInto == null ? | |
writeStdOut (output) : | |
chain (writeFile (program.insertInto)) | |
(map (replace (delimiters) ('$1\n\n' + output + '\n$2')) | |
(readFile (program.insertInto)))))); |
(e => { console.error (String (e)); process.exit (1); }) | ||
(_ => { process.exit (0); }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You know what the type of the thing your Future will reject with is, so you may not need the string cast.
(e => { console.error (String (e)); process.exit (1); }) | |
(_ => { process.exit (0); }) | |
(e => { console.error (e.stack); process.exit (1); }) | |
(_ => { process.exit (0); }) |
I wrote and ran a script to verify that these changes do not change the output for a handful of projects.