Idioms aside, what is the material benefit of using volatile over a locally 
scoped atom? I googled a bit and it seems that it has less concurrency 
guarantees and thus less overhead in Clojure but for ClojureScript is it really 
any lighter than an atom? 



Sent from my iPhone

> On Jun 22, 2015, at 11:36 AM, Francis Avila <fav...@breezeehr.com> wrote:
> 
> Yes, this is fine.
> 
> It is slightly more idiomatic to use volatile! (introduced in Clojure 1.7 and 
> recent ClojureScripts) if you just need local mutation and don't intend on 
> sharing the atom:
> 
> 
> (defn load-url-async 
>  [url success error] 
>  (let [loader (.get http url (fn [res] 
>                                (let [buffer (volatile! "")] 
>                                  (.on res "data" #(vswap! buffer str %)) 
>                                  (.on res "end" #(success @buffer)))] 
>    (.on loader "error" error)) 
> 
>> On Monday, June 22, 2015 at 11:06:32 AM UTC-5, Honza Břečka wrote:
>> Hi, I'm not sure if I can rewrite the following piece of JavaScript 
>> targeting node:
>> 
>> var loadUrl = function(url, success, error) {
>>  http.get(url, function(res) {
>>    var buffer = '';
>>    res.on('data', function(chunk) {
>>      buffer += chunk;
>>    });
>>    res.on('end', function() {
>>      success(buffer);
>>    });
>>  }).on('error', function(e) {
>>    error(e);
>>  });
>> };
>> 
>> to this:
>> 
>> (defn load-url-async
>>  [url success error]
>>  (let [loader (.get http url (fn [res]
>>                                (let [buffer (atom "")]
>>                                  (.on res "data" #(swap! buffer str %))
>>                                  (.on res "end" #(success @buffer)))]
>>    (.on loader "error" error))
>> 
>> Can someone experienced confirm that I can use atom inside the function?
>> 
>> Thanks
> 
> -- 
> Note that posts from new members are moderated - please be patient with your 
> first post.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "ClojureScript" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojurescript+unsubscr...@googlegroups.com.
> To post to this group, send email to clojurescript@googlegroups.com.
> Visit this group at http://groups.google.com/group/clojurescript.

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojurescript+unsubscr...@googlegroups.com.
To post to this group, send email to clojurescript@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to