Skip to content

Instantly share code, notes, and snippets.

@RichAyotte
Created February 6, 2014 23:30
Show Gist options
  • Save RichAyotte/8854702 to your computer and use it in GitHub Desktop.
Save RichAyotte/8854702 to your computer and use it in GitHub Desktop.
Ruby array iterator in JavaScript using Sweet.js
// Ever wish you could iterate JavaScript arrays like you do in Ruby?
// Well you can with this sweet.js macro.
macro do {
rule infix { $lh.each | _ $rh _ $body ... end} => {
$lh.forEach(function($rh){
$($body) ...
});
}
}
// Now you can do:
var a = ['Yukihiro', 'Matsumoto'];
a.each do |i|
console.debug('Hello ' + i);
end
// Try it in their editor
// http://sweetjs.org/browser/editor.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment