Live data from Hacker News

Clojure core.async and Go: A Code Comparison

blog.drewolson.org

21–30 of 81 posts

Re: Clojure core.async and Go: A Code Comparison

#21
post #12

Earlier quoted context omitted.

Yeah, Thread/sleep isn't quite what you want here: (time (let [c (chan), n 1000] (dotimes [i n] (go (Thread/sleep 50) (>! c i))) (dotimes [i n] ( "Elapsed time: 8412.25733 msecs" (defmacro gosleep [millis] `( (time (let [c (chan), n 1000] (dotimes [i n] (go (gosleep 50) (>! c i))) (dotimes [i n] ( "Elapsed time: 91.278469 msecs" ETA: for comparison, here's what happens if you actually make 1000 system threads: (time…

Thanks (and thanks to pron). I've updated the post to include a warning against using Thread/sleep in go blocks for "real" code.

I feel silly though that this is the one thing I've commented on, so:

This is an awesome post, thanks for it ^_^

Re: Clojure core.async and Go: A Code Comparison

#22
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.

I think the easiest fix is to make the channel buffered. "make(chan string)" => "make(chan string, 1)" Not sure if there is a more idiomatic golang way to accomplish this.

Re: Clojure core.async and Go: A Code Comparison

#23
post #8

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 'i'. In the clojure version, it appears that the value of 'i' is part of the closure for that function. The go version does what it does to avoid a race condition, because the goroutines are being spun up in the background and it's highly likely that it will take longer to spin…

The i is scoped differently in clojure than in golang so it doesn't have the same potential pitfall. I think golang people consider their scoping here a mistake (and have tooling to check it?).

Also see Javascript's IIFE: http://en.wikipedia.org/wiki/Immediately-invoked_function_ex...

Re: Clojure core.async and Go: A Code Comparison

#26

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. I think the easiest fix is to make the channel buffered. "make(chan string)" => "make(chan string, 1)" Not sure if there is a more idiomatic golang way to accomplish this.

Once main exits the program does as well, so all other goroutines will be terminated.

Re: Clojure core.async and Go: A Code Comparison

#27
post #4

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?

terseness is not a feature Go is striving for. That being said, it's still more terse than most other statically typed languages, simply due to type inference. You do have to write out some loops that other languages provide syntactic sugar for... people from those languages call that verbosity. Gophers call it clarity :)

Re: Clojure core.async and Go: A Code Comparison

#28
post #27
post #4

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?

terseness is not a feature Go is striving for. That being said, it's still more terse than most other statically typed languages, simply due to type inference. You do have to write out some loops that other languages provide syntactic sugar for... people from those languages call that verbosity. Gophers call it clarity :)

Well, it's not so much syntactic sugar as having the power to abstract them away via generic functions like map, which I'd argue are both more clear and less error prone.

Re: Clojure core.async and Go: A Code Comparison

#29
This is very cool and intressting. I have to think about something to do with core.async.

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.

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.

Re: Clojure core.async and Go: A Code Comparison

#30
Quick question for anyone here familiar with core.async:

Would it be possible (and if so what would be the simplest way) to implement something like Python's generators and `yield` statement in Clojure using core.async? I'm thinking something like:

    (defn range [n]
      (generator
        (loop [i 0]
          (yield i)
          (when ( 0
      (generator) ;; => 1
      ;; etc
      )
Post reply on HN