Created
February 6, 2014 23:30
-
-
Save RichAyotte/8854702 to your computer and use it in GitHub Desktop.
Ruby array iterator in JavaScript using Sweet.js
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
// 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