Explanation of: https://twitter.com/katyemoe/status/677122509645455360
function doubleSay (str) {
return str + ", " + str;
}
function capitalize (str) {
return str[0].toUpperCase() + str.substring(1);
}
function exclaim (str) {
return str + '!';
}
This covers the basic use case:
function pipeline(input, ...methods) {
for (let method of methods) {
input = method(input);
}
return input;
}
let result = pipeline('hello', doubleSay, capitalize, exclaim)
Would be the same as:
let result = "hello"
|> doubleSay
|> capitalize
|> exclaim;
Multiple arguments could be solved like:
let newScore = pipeline(
person.score,
double,
_ => add(7, _),
_ => boundScore(0, 100, _)
)
Would be the same as:
let newScore = person.score
|> double
|> _ => add(7, _)
|> _ => boundScore(0, 100, _);
This could also work async too with something like:
function pipeline(input, ...methods) {
for (let method of methods) {
input = await Promise.resolve(method(input));
}
return input;
}