TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Writing Your First Sweet.js Macro

37 pointsby jlongsterover 11 years ago

4 comments

andreypoppover 11 years ago
I found sweet.js so much useful for scraping boilerplate out of my code. Example — sweet-assertions <a href="https://github.com/andreypopp/sweet-assertions" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;andreypopp&#x2F;sweet-assertions</a> — a set of macros to generate assertions with nice failure messages.<p>Edit: another experimental macros — <a href="https://github.com/andreypopp/react-macros" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;andreypopp&#x2F;react-macros</a> — a set of syntax extensions for writing React (<a href="http://facebook.github.io/react" rel="nofollow">http:&#x2F;&#x2F;facebook.github.io&#x2F;react</a>) components.
评论 #7220017 未加载
davexunitover 11 years ago
I tried a simple one. Here is my interpretation of Scheme&#x27;s delay&#x2F;force macros: <a href="https://www.refheap.com/36606" rel="nofollow">https:&#x2F;&#x2F;www.refheap.com&#x2F;36606</a> (forgive the messed up indentation...)
santialboover 11 years ago
I had so much fun playing with this. Here&#x27;s the code I wrote for a simple scala-like for<p><pre><code> macro for { rule { {$x &lt;- $y:expr;} {$z:expr} } =&gt; { $y.forEach(function($x) { $z }); } rule { {$x &lt;- $y:expr; $($a &lt;- $b:expr) (;) ... } {$z:expr} } =&gt; { $y.forEach(function($x) { for { $($a &lt;- $b;) ... } { $z } }) } } for { i &lt;- [1,2,3]; j &lt;- [3,2,1]; k &lt;- [5,5,5]; } { console.log(i + j + k) }</code></pre>
etrinhover 11 years ago
The interactive macro editor is a great touch. I don&#x27;t know if this is prevalent, but this is the first time I&#x27;ve seen a tutorial interactively modify code in the editor <i>for you</i>. Instead of &quot;here, type this into the editor above and see what happens,&quot; there will be a link to &quot;click here to see what happens&quot; and it will change the code in the editor accordingly. I think this is a good thing, because I rarely go and do the things that tutorials suggest I do, even if they are as simple as typing into an editor that is 2 inches above the text I am reading.