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 core.async and Go: A Code Comparison

150 pointsby drewolsonalmost 12 years ago

13 comments

nickikalmost 12 years ago
This is very cool and intressting. I have to think about something to do with core.async.<p>This is also a good example why macros are just awesome. Go is a language design to work well with goroutins and channels, but the clojure code looks just as good and readable. You could simply not have such idiomatic use of these concepts without macros.<p>Or am I wrong, can a flexible language like scala or python be extended to look as good for that usecase? I dont know enougth of the tricks scala people use, I cant juge if it is possible.
评论 #5993517 未加载
评论 #5993017 未加载
kinofcainalmost 12 years ago
Are the first two examples equivalent? In the go version, an anonymous function is being declared and then called with the value of the outer &#x27;i&#x27;. In the clojure version, it appears that the value of &#x27;i&#x27; is part of the closure for that function.<p>The go version does what it does to avoid a race condition, because the goroutines are being spun up in the background and it&#x27;s highly likely that it will take longer to spin up at least one of them than it will to finish the loop, so without it you&#x27;d likely get output of all nines.<p>If the clojure version doesn&#x27;t have that problem, then I think it&#x27;s a somewhat telling indication of how it&#x27;s actually working.<p>TL&#x2F;DR: The Go version has to work around the race condition because the runtime is doing things &quot;right&quot;, is core.async?
评论 #5992081 未加载
评论 #5992032 未加载
hueypalmost 12 years ago
Are the clojure threads actually lightweight? Thread&#x2F;sleep is blocking so you&#x27;d need to occupy 10 threads right? If you wanted to sleep without blocking a real thread would you use an executor service to write to a channel after delay and block on that?
评论 #5992025 未加载
评论 #5994320 未加载
simpleglideralmost 12 years ago
Small bug irrelevant to the main point of the article: The very last golang example has a leak. If the timeout does occur, main will exit after a timeout and the spawned go routine will hang on the channel push.<p>I think the easiest fix is to make the channel buffered. &quot;make(chan string)&quot; =&gt; &quot;make(chan string, 1)&quot; Not sure if there is a more idiomatic golang way to accomplish this.
评论 #5992334 未加载
aphyralmost 12 years ago
By the way, you can use (dotimes [i 100]) in place of (doseq [i (range 100)]).
mjwalmost 12 years ago
Quick question for anyone here familiar with core.async:<p>Would it be possible (and if so what would be the simplest way) to implement something like Python&#x27;s generators and `yield` statement in Clojure using core.async? I&#x27;m thinking something like:<p><pre><code> (defn range [n] (generator (loop [i 0] (yield i) (when (&lt; i n) (recur (inc i))) (let [generator (range 5)] (generator) ;; =&gt; 0 (generator) ;; =&gt; 1 ;; etc )</code></pre>
评论 #5992674 未加载
评论 #5992657 未加载
评论 #5993321 未加载
SeanDavalmost 12 years ago
I have never used Go or Clojure, but this is the first time I have heard of Go as being verbose - or is that just in comparison to Clojure?
评论 #5992115 未加载
评论 #5992648 未加载
评论 #5992084 未加载
评论 #5992014 未加载
评论 #5993510 未加载
评论 #5992053 未加载
评论 #5992504 未加载
评论 #5993670 未加载
codygmanalmost 12 years ago
This is pretty awesome. I&#x27;m waiting on a clojure book in the mail, but this article makes me want to dive in sooner!<p>Being a Go fan, this definitely draws me to clojure as a language since the other language I&#x27;ve been messing with was racket (a scheme lisp).
bjeanesalmost 12 years ago
Thanks for the write up. Definitely good to see core.async and goroutines side by side...
knodialmost 12 years ago
Totally off topic, when i see &quot;)))))))&quot; it makes me want to run the other way.
评论 #5993298 未加载
评论 #5994406 未加载
mortdeusalmost 12 years ago
-&gt; is not a valid operator in go.
charlieflowersalmost 12 years ago
FYI, there&#x27;s a typo: &quot;it’s on yet available&quot; -&gt; &quot;it’s NOT yet available&quot;
egeozcanalmost 12 years ago
In Go, am I supposed to seed the rand manually? <a href="http://i.imgur.com/Rw5afx9.png" rel="nofollow">http:&#x2F;&#x2F;i.imgur.com&#x2F;Rw5afx9.png</a>
评论 #5992733 未加载