Skip to content

Instantly share code, notes, and snippets.

@grncdr
Last active January 3, 2016 02:08
Show Gist options
  • Save grncdr/8393328 to your computer and use it in GitHub Desktop.
Save grncdr/8393328 to your computer and use it in GitHub Desktop.
This acts sort of like a thread first. Instead of actually modifying the arguments list, I implicitly assign the result of each line in the body to `$_` then you can use `$_` in the body to refer to the result of the last line.
macro thread {
case { $name ($args (,) ...) { $body ... } } => {
var result = makeIdent('$_', #{$name})
return withSyntax($$_ = [result]) {
console.log('here')
return #{
(function () {
var $$_ = arguments.length > 1 ? [].slice.call(arguments) : arguments[0];
thread_body $$_ $body ...;
return $$_
})($args (,) ...)
}
}
}
}
macro thread_body {
rule {
$_ $first ...;
$rest ...
} => {
$_ = $first ...;
thread_body $_ $rest ...
}
rule {} => {}
}
var y = thread (1) {
second(1, $_, 3);
last(something, $_)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment