Hacker News new | past | comments | ask | show | jobs | submit login
Clojure, Multi-core, AWS Cluster Compute & Lattes (dosync.posterous.com)
52 points by brisance on July 14, 2010 | hide | past | favorite | 9 comments



From the article:

   (defn ping []
     (-> (http/get "http://localhost:5123" :as :string) 
         :content))

    (defn multi-ping []
      (->> (repeatedly 16 #(ping))
           (apply str)))
Can anyone tell me what --> and ->> do in this context? I've been trying to find out myself but given that they're both impossible to search for on Google...


They're called "thrush" operators, and they translate that code to this:

   (defn ping []
     (:content (http/get "http://localhost:5123" :as :string)))

    (defn multi-ping []
      (apply str (repeatedly 16 #(ping)))
More examples: http://debasishg.blogspot.com/2010/04/thrush-in-clojure.html


They're called "thrush" but many of us pronounce them "thread" and "thread end".

(-> x expr1 expr2 ... exprN) evaluates x, then takes the result and puts it as the first argument in expr1, evaluates that, and puts the result in the first argument of expr2, repeating until exprN, where the result is returned.

(->> x expr1 expr2 exprN) does the same thing, but instead of making it the first argument it makes it the last argument of subsequent expressions.

Clojure's culture generally agrees that the "subject" of function calls should be the first argument, so in general the -> operator is all you need. Sometimes you're shuttling data around, and then ->> comes into play.

Please note that multi-ping in this is atypical and constructed that way for didactic purposes (i.e., to be transformed later). Usually you'd see it as:

     (defn multi-ping [] (apply str (repeat 16 ping)))
For readability, some people might prefer to use a let. I think with only 2 levels of nesting, it should be pretty easy to see what's up and I wouldn't bother, but that'd look like:

    (defn [] multi-ping
        (let [pings (repeat 16 ping)]
            (apply str pings)))


I went ahead and changed my code to use let. I agree there's no reason to throw people off on such a simple snippet.


Probably a good idea, but thanks anyway for introducing me to another cool feature of Clojure!



I was confused by:

> In fact for $3.20 (the price of a latte) I can play around with Clojure code on a high performance 8-core 64bit server for two hours.

An m2.4xlarge spot instance is ~$0.80/hr and has "26 EC2 Compute Units (8 virtual cores with 3.25 EC2 Compute Units each)".

Are the cluster instances that much closer to bare metal than 'normal' ec2 instances or am I missing something?


33.5 EC2 compute units and very low latency between instances. More details here:

http://aws.amazon.com/ec2/hpc-applications/


I suppose he's going for the 16 cores instead of 8 given the interconnect isn't used. It would be interesting to see a performance comparison between the xlarges and the cluster instances on simple speedups like this.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: