Functional JavaScript: simple function pipeline creators
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
let pipe = function() { | |
let functions = arguments, | |
intermediate = undefined; | |
return function() { | |
intermediate = functions[0].apply(this, arguments); | |
for(let i = 1; i < functions.length; i++) { | |
intermediate = functions[i](intermediate); | |
} | |
return intermediate; | |
}; | |
}; | |
// Example | |
let concat = function(a, b) { | |
return a + ' ' + b; | |
}, | |
format = function(message) { | |
return message.charAt(0).toUpperCase() + message.slice(1).toLowerCase(); | |
}, | |
period = function(message) { | |
return message.charAt(message.length - 1) !== '.' ? message + '.' : message; | |
}, | |
pipedFunction = pipe(concat, format, period); | |
let result = pipedFunction('hello world,', 'this message has been piped'); | |
console.log(result); // "Hello world, this message has been piped." |
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
let pipeI = function() { | |
let functions = arguments; | |
return function * pipe() { | |
let i = 0, intermediate = functions[i++].apply(this, arguments); | |
yield intermediate; | |
for(; i < functions.length; i++) { | |
intermediate = functions[i].call(this, intermediate); | |
yield intermediate; | |
} | |
} | |
}; | |
// Example | |
let concat = function(a, b) { | |
return a + ' ' + b; | |
}, | |
format = function(message) { | |
return message.charAt(0).toUpperCase() + message.slice(1).toLowerCase(); | |
}, | |
period = function(message) { | |
return message.charAt(message.length - 1) !== '.' ? message + '.' : message; | |
}, | |
pipedFunction = pipeI(concat, format, period); | |
let iterator = pipedFunction('hello world,', 'this message has been piped iteratively'); | |
console.log(iterator.next().value); // "hello world, this message has been piped iteratively" | |
console.log(iterator.next().value); // "Hello world, this message has been piped iteratively" | |
console.log(iterator.next().value); // "Hello world, this message has been piped iteratively." |
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
let pipeP = function() { | |
let functions = arguments, | |
recursivePromise = function(value, i) { | |
return new Promise((resolve, reject) => { | |
functions[i](value) | |
.then(function(intermediate) { | |
i += 1; | |
if(i < functions.length) { | |
recursivePromise(intermediate, i).then(resolve, reject); | |
} else { | |
resolve(intermediate); | |
} | |
}, reject); | |
}); | |
}; | |
return function() { | |
let i = 1; | |
return new Promise((resolve, reject) => { | |
functions[0].apply(this, arguments) | |
.then((intermediate) => { | |
recursivePromise(intermediate, i).then(resolve, reject); | |
}, reject); | |
}); | |
}; | |
}; | |
// Example | |
let concat = function(a, b) { | |
let v = a + ' ' + b; | |
return Promise.resolve(v); | |
}, | |
format = function(message) { | |
let v = message.charAt(0).toUpperCase() + message.slice(1).toLowerCase(); | |
return Promise.resolve(v); | |
}, | |
period = function(message) { | |
let v = message.charAt(message.length - 1) !== '.' ? message + '.' : message; | |
return Promise.resolve(v); | |
}, | |
pipedFunction = pipeP(concat, format, period); | |
pipedFunction('hello world,', 'this message has been piped using Promises') | |
.then((message) => console.log(message)); // "Hello world, this message has been piped using promises." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment