Last active
September 4, 2016 15:43
-
-
Save MarkusPfundstein/319d4f692bb3bf8234949b3f6be2ad3d to your computer and use it in GitHub Desktop.
Beautiful ES6
This file contains 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 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