Skip to content

Instantly share code, notes, and snippets.

@MarkusPfundstein
Last active September 4, 2016 15:43
Show Gist options
  • Save MarkusPfundstein/319d4f692bb3bf8234949b3f6be2ad3d to your computer and use it in GitHub Desktop.
Save MarkusPfundstein/319d4f692bb3bf8234949b3f6be2ad3d to your computer and use it in GitHub Desktop.
Beautiful ES6
const fs = require('fs');
const head = ([x, ...t]) => x;
const tail = ([x, ...t]) => t;
const compose = (f, ...rest) => (x) =>
rest.length === 1
? head(rest)(f(x))
: compose(head(rest), ...tail(rest))(f(x));
const promiseCb = (resolve, reject) => (error, data) => error ? reject(error) : resolve(data);
const promisify = (fun) => (...args) => new Promise((res, rej) => fun(...args, promiseCb(res, rej)));
const readFile = promisify(fs.readFile);
const bufToString = (x, encoding) => x.toString(encoding);
const bufToUTF8 = (x) => bufToString(x, 'utf-8');
const splitString = (x, delim) => x.split(delim);
const splitStringNewLine = (x) => splitString(x, '\n')
const filterEmpty = (x) => x.filter((el) => el.length > 0);
readFile('./text.txt')
.then(
compose(
bufToUTF8,
splitStringNewLine,
filterEmpty,
console.log))
.catch(error => console.error(error.stack));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment