Last active
August 19, 2016 15:33
-
-
Save WreckedAvent/9aa698d3e9e6a46c5c62c1e266c95f20 to your computer and use it in GitHub Desktop.
sweet js ML-style auto-curried function literals
This file contains hidden or 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
// Auto-curried fun macro, inspired by ML syntax | |
// | |
// Examples: | |
// let add = fun x y { x + y } | |
// let map = fun f x { x.map(f) } | |
syntax fun = ctx => { | |
const ids = [] | |
let ret = null | |
// start parsing our context | |
for (let value of ctx) { | |
// but we only want identifiers ... | |
if (value.isIdentifier()) { | |
ids.unshift(value) | |
} | |
// ... and the first set of braces afterwards | |
if (value.isBraces()) { | |
ret = value.inner() // get all of the stuff inside the braces | |
break // after the first set of braces, our macro is over; stop parsing | |
} | |
} | |
// because we unshifted our identifiers, we can iterate over them | |
for (let value of ids) { | |
// and just wrap our return value in a function for each identifier | |
ret = #`function(${value}) { return ${ret}; }` | |
} | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment