Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
const fs = require('fs');
const pipe = (...fns) => (x) => fns.reduce((y, f) => f(y), x);
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) return fn(...args);
return function (a) {
return curried(...[...args, a]);
const handleError = (err) => {
if (err) throw err;
const datetime = {
toString: () => new Date().toISOString(),
// The three functions that are the building blocks of the logging solution.
// Building block 1: formatLog
function formatLog(datetime, severity, message) {
return `${datetime} [${severity}] - ${message}`;
// Building block 2: writeToConsole
function writeToConsole(message) {
return message;
// Building block 3: writeToFile
function writeToFile(filePath, message) {
fs.appendFile(filePath, message + '\n', handleError);
return message;
// Calling the building blocks using nesting: no composition:
// Write a log message to the console:
writeToConsole(formatLog(new Date().toISOString(), 'INFO', 'Hello World'));
// Write a log message to a file:
formatLog(new Date().toISOString(), 'INFO', 'Hello World')
// Write a log message to both the console and a file:
writeToConsole(formatLog(new Date().toISOString(), 'INFO', 'Hello World'))
// Let's compose using currying and piping:
const curriedFormatLog = curry(formatLog);
const formatLogInfo = curriedFormatLog(datetime)("INFO");
const curriedWriteToFile = curry(writeToFile);
const writeToLogFile = curriedWriteToFile('/path/to/file');
// Pipe (or chain) some functions into a new function that does logging for us:
const logInfoToFile = pipe(formatLogInfo, writeToConsole, writeToLogFile);
// Let's do some logging:
logInfoToFile("Hello World");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment