Created
May 29, 2019 23:30
-
-
Save sbrl/c485d924964fa54c233bb1e275a28909 to your computer and use it in GitHub Desktop.
Pipe operator parrotfill based on https://hacks.mozilla.org/2019/05/javascript-and-evidence-based-language-design/
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
"use strict"; | |
async function pipe(initial_value, ...funcs) { | |
let current = initial_value; | |
for(let func of funcs) { | |
current = func(current); | |
// If it's thenable, then it's probably a Promise | |
if(typeof current.then == "function") | |
current = await current; | |
} | |
return current; | |
} | |
function add(a, b) { return a + b; } | |
function make_message(x) { return `item: ${x}`; } | |
async function tokenise(x) { return x.split(/\s+/g); } | |
pipe( | |
10, | |
add.bind(null, 5), | |
make_message, | |
tokenise | |
).then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment