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.

Clojure from the ground up: Macros

117 pointsby llambdaover 11 years ago

4 comments

juliangambleover 11 years ago
One of the most powerful uses of Clojure&#x27;s macro system has been in the core.async library. This has enabled the construction of a library with operators similar to Go&#x27;s Communicating Sequential Processes [Hoare 1978].<p>What is different about Clojure&#x27;s implementation is that no changes to the compiler were required - &#x27;deep walking macros&#x27; (<a href="http://blog.fogus.me/2013/07/17/an-introduction-to-deep-code-walking-macros-with-clojure/" rel="nofollow">http:&#x2F;&#x2F;blog.fogus.me&#x2F;2013&#x2F;07&#x2F;17&#x2F;an-introduction-to-deep-code...</a>) were used - that actually restructure the code.<p>The benefit of this in the library is that when a process is blocked, the wait is converted to data and the thread is freed up and returned to the pool. You can have multiple processes talking to each other with a thread-pool size of one. (And still reason about the code in a neat way).<p>The benefit of this comes in the JavaScript environment, (via ClojureScript) where you can compile code that you would ordinarily coordinate with threads using CSP. (With Go-lang style operators)<p>You can see an example of this on David Nolen&#x27;s pages here: * 10,000 processes <a href="http://swannodette.github.io/2013/08/02/100000-processes/" rel="nofollow">http:&#x2F;&#x2F;swannodette.github.io&#x2F;2013&#x2F;08&#x2F;02&#x2F;100000-processes&#x2F;</a> * 100,000 updates <a href="http://swannodette.github.io/2013/08/02/100000-dom-updates/" rel="nofollow">http:&#x2F;&#x2F;swannodette.github.io&#x2F;2013&#x2F;08&#x2F;02&#x2F;100000-dom-updates&#x2F;</a><p>This is the power of macros in Clojure - things that would require a compiler extension in another language, are a library in Clojure.
评论 #6822162 未加载
评论 #6822304 未加载
jlongsterover 11 years ago
These articles tend to miss a new development in JavaScript: sweet.js (<a href="http://sweetjs.org/" rel="nofollow">http:&#x2F;&#x2F;sweetjs.org&#x2F;</a>), hygienic macros for JavaScript. It is essentially scheme&#x27;s syntax-case&#x2F;rules completely for JS, and it&#x27;s done <i>really</i> well.
评论 #6822642 未加载
评论 #6822712 未加载
groovy2shoesover 11 years ago
Code-walking macros are very powerful, but there&#x27;s a performance caveat:<p><a href="https://code.google.com/p/chibi-scheme/issues/detail?id=114#c3" rel="nofollow">https:&#x2F;&#x2F;code.google.com&#x2F;p&#x2F;chibi-scheme&#x2F;issues&#x2F;detail?id=114#...</a><p>For most macros, this won&#x27;t be an issue. But if you wind up with code-walking macros than can be nested, you need to be mindful of the nesting or your code might take a while to compile.
film42over 11 years ago
Superb set of examples, thank you very much :)