TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Clojure core.async and Go: A Code Comparison

150 点作者 drewolson将近 12 年前

13 条评论

nickik将近 12 年前
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 未加载
kinofcain将近 12 年前
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 未加载
hueyp将近 12 年前
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 未加载
simpleglider将近 12 年前
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 未加载
aphyr将近 12 年前
By the way, you can use (dotimes [i 100]) in place of (doseq [i (range 100)]).
mjw将近 12 年前
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 未加载
SeanDav将近 12 年前
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 未加载
codygman将近 12 年前
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).
bjeanes将近 12 年前
Thanks for the write up. Definitely good to see core.async and goroutines side by side...
knodi将近 12 年前
Totally off topic, when i see &quot;)))))))&quot; it makes me want to run the other way.
评论 #5993298 未加载
评论 #5994406 未加载
mortdeus将近 12 年前
-&gt; is not a valid operator in go.
charlieflowers将近 12 年前
FYI, there&#x27;s a typo: &quot;it’s on yet available&quot; -&gt; &quot;it’s NOT yet available&quot;
egeozcan将近 12 年前
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 未加载