Skip to content

Instantly share code, notes, and snippets.

@omas-public
Created March 24, 2025 08:28
Show Gist options
  • Save omas-public/d8131e3ad64710fbd992892c53799057 to your computer and use it in GitHub Desktop.
Save omas-public/d8131e3ad64710fbd992892c53799057 to your computer and use it in GitHub Desktop.
const fn = value => {
return value
}
// main function
;((stdin, stdout) => {
// input
const value = stdin('/etc/passwd').readStream()
// call function
const result = fn(value)
// output
stdout(result).toStream()
})(std().in, std().out)
function std () {
const identity = value => value
return {
in: (file = '/dev/stdin') => {
const split =
(sep = ' ') =>
string =>
string.split(sep)
const toArray = sep => fn => iter => Array.from(split(sep)(iter), fn)
const stream = require('fs').readFileSync(file, 'utf-8').toString().trim()
return {
readCols: (fn = identity) => toArray(' ')(fn)(stream),
readRows: (fn = identity) => toArray('\n')(fn)(stream),
readStream: (fn = identity) => fn(stream),
readMatrix: (fn = identity) =>
toArray('\n')(identity)(stream).map(toArray(' ')(fn))
}
},
out: value => {
const _p = fn => value => console.log(fn(value))
const join =
(sep = '\n') =>
array =>
array.join(sep)
return {
toCols: () => _p(join(' '))(value),
toRows: () => _p(join('\n'))(value),
toJSON: () => _p(JSON.stringify)(value),
toStream: () => _p(identity)(value),
toMatrix: () => _p(join('\n'))(value.map(join(' ')))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment