Re: Avoid duplicate computation in commute?

2012-05-18 Thread nicolas.o...@gmail.com
I think part of the problem is that commute should not return any result. You are not sure this is actually the value that will be returned, this is costly and misleading. There should be at least a variant of commute that return nil. -- You received this message because you are subscribed to

Re: Avoid duplicate computation in commute?

2012-05-17 Thread DAemon
; > wrong, *throws question to the Gods of clojure* > > > > > > > > > > > > > > > > On Fri, May 18, 2012 at 8:03 AM, Warren Lynn > wrote: > > > Thanks and these are certainly workable solutions. But is there any > > > particular reason

Re: Avoid duplicate computation in commute?

2012-05-17 Thread Warren Lynn
r as I know. Although I could be > wrong, *throws question to the Gods of clojure* > > > > > > > > On Fri, May 18, 2012 at 8:03 AM, Warren Lynn wrote: > > Thanks and these are certainly workable solutions. But is there any > > particular reason why with commute t

Re: Avoid duplicate computation in commute?

2012-05-17 Thread DAemon
way to tell that no other thread has changed the Ref, as far as I know. Although I could be wrong, *throws question to the Gods of clojure* On Fri, May 18, 2012 at 8:03 AM, Warren Lynn wrote: > Thanks and these are certainly workable solutions. But is there any > particular reason why with commut

Re: Avoid duplicate computation in commute?

2012-05-17 Thread Warren Lynn
Thanks and these are certainly workable solutions. But is there any particular reason why with commute the update function is ALWAYS called twice, even when no other thread changed the ref value at the commit time? That is what bothers me. On May 17, 5:46 pm, DAemon wrote: > Would some of t

Re: Avoid duplicate computation in commute?

2012-05-17 Thread DAemon
I think the point with `commute` is to allow for more concurrency at the > expense of more computation. > > If you want assurance that your function is only called once, you can use > `alter`. > > Keep in mind that *any* code in a Ref transaction has the potential to be > call

Re: Avoid duplicate computation in commute?

2012-05-17 Thread Stuart Sierra
I think the point with `commute` is to allow for more concurrency at the expense of more computation. If you want assurance that your function is only called once, you can use `alter`. Keep in mind that *any* code in a Ref transaction has the potential to be called more than once if there

Avoid duplicate computation in commute?

2012-05-17 Thread Warren Lynn
Hi, The duplicate computation in "commute" seems to me quite a blemish on clojure's beauty. I even see somebody's code with comments "pre- compute x because otherwise commute will call it twice". So I need to keep that fact in the back of my mind when using it? Tha

Re: Why can not alter or ref-set ref after commute it?

2010-07-25 Thread dennis
01 pm, Moritz Ulrich wrote: > Read the documentation of commute > carefully:http://richhickey.github.com/clojure/clojure.core-api.html#clojure.co... > > commute acts at the end of the current dosync-block, regardless of > when commute was applied inside it. That's the reason why you c

Re: Why can not alter or ref-set ref after commute it?

2010-07-25 Thread Moritz Ulrich
Read the documentation of commute carefully: http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/commute commute acts at the end of the current dosync-block, regardless of when commute was applied inside it. That's the reason why you can't ref-set it after a co

Why can not alter or ref-set ref after commute it?

2010-07-25 Thread dennis
Alter or ref-set a ref after commute would throw a IllegalStateException:Can't set after commute for example: user=> (def counter (ref 0)) #'user/counter (dosync (commute counter inc) (ref-set counter 3)) java.lang.IllegalStateException: Can't set after commute (NO_SOURCE_FILE:

Re: Equivalent of commute without return value?

2010-05-04 Thread Nicolas Oury
Dear all, >> >> Is there an equivalent of commute without return value? >> >> A part of the usage of commute just want to change something without >> knowing the result. >> (For example, I want to extend a set). >> >> I know it is not very expensive

Re: Equivalent of commute without return value?

2010-05-04 Thread Rich Hickey
On May 4, 2010, at 9:30 AM, Nicolas Oury wrote: Dear all, Is there an equivalent of commute without return value? A part of the usage of commute just want to change something without knowing the result. (For example, I want to extend a set). I know it is not very expensive but computing

Equivalent of commute without return value?

2010-05-04 Thread Nicolas Oury
Dear all, Is there an equivalent of commute without return value? A part of the usage of commute just want to change something without knowing the result. (For example, I want to extend a set). I know it is not very expensive but computing twice a function - if there is a bit of contention, but

Re: commute misunderstanding

2010-02-19 Thread Аркадий Рост
It seems I have an example that shows difference between commute and alter. Here it is: (defn my-inc [x] (dosync (Thread/sleep 1000) (inc x))) alter*** (def x (ref 0)) (do (def fut (future (do (Thread/sleep 300) (dosync (alter x inc)) @x))) (dosync (println @x)(alter x my-inc

Re: commute misunderstanding

2010-02-18 Thread Laurent PETIT
2010/2/18 Аркадий Рост : > hmm..but in what way I can write code to demonstrate the difference? You must reproduce the race conditions. An idea is to start 2 transactions in 2 different threads, while making the first one sleep sufficiently so you're sure the second one will commit first. Simply

Re: commute misunderstanding

2010-02-18 Thread Аркадий Рост
hmm..but in what way I can write code to demonstrate the difference? -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with

Re: commute misunderstanding

2010-02-18 Thread Laurent PETIT
2010/2/18 Аркадий Рост : > Hi! > I don't understand difference between commute and alter. In fact I > don't understand how can I get different results by changing alter to > commute in code.I've read that the in-transaction value with commute > is that it may differ

commute misunderstanding

2010-02-18 Thread Аркадий Рост
Hi! I don't understand difference between commute and alter. In fact I don't understand how can I get different results by changing alter to commute in code.I've read that the in-transaction value with commute is that it may differ from the post-commit value, as the effect of commut

ensure and commute

2009-08-17 Thread Mark Volkmann
B commits its change to Ref R. 5) Transaction A calls ensure on Ref R which succeeds because no currently running transaction has modified Ref R. 6) Transaction A calls commute on Ref R. 7) Transaction A tries to commit. 8) Transaction A will retry because wasEnsured is true and the latest comm

Re: commute and ref-set

2009-08-17 Thread Mark Volkmann
On Mon, Aug 17, 2009 at 2:34 AM, Christophe Grand wrote: > Hi Mark and Meikel, > > On Mon, Aug 17, 2009 at 8:15 AM, Meikel Brandmeyer wrote: >> >> On Aug 17, 3:23 am, Mark Volkmann wrote: >> > Maybe it's there in case ref-set or alter was called on the Ref bef

Re: commute and ref-set

2009-08-17 Thread Christophe Grand
Hi Mark and Meikel, On Mon, Aug 17, 2009 at 8:15 AM, Meikel Brandmeyer wrote: > > On Aug 17, 3:23 am, Mark Volkmann wrote: > > Maybe it's there in case ref-set or alter was called on the Ref before > commute. > > In that case, does that mean that the commute functions

Re: commute and ref-set

2009-08-16 Thread Meikel Brandmeyer
Hi, On Aug 17, 3:23 am, Mark Volkmann wrote: > Once commute has been called on a Ref (within a transaction of > course), ref-set and alter cannot be called on it within the same > transaction. > This is enforced by the following code from the doSet method in > LockingT

commute and ref-set

2009-08-16 Thread Mark Volkmann
Once commute has been called on a Ref (within a transaction of course), ref-set and alter cannot be called on it within the same transaction. This is enforced by the following code from the doSet method in LockingTransaction.java. if(commutes.containsKey(ref)) throw new

Re: commute documentation detail

2009-08-03 Thread Rich Hickey
On Mon, Aug 3, 2009 at 5:44 AM, Christophe Grand wrote: > I think Mark's code exhibits a bug: > > (def my-ref (ref 1)) > > (dosync >   (ref-set my-ref 5) >   (commute my-ref inc)) ; returns 6 > > (println "my-ref =" @my-ref) ; prints 7 > > since my

Re: commute documentation detail

2009-08-03 Thread Christophe Grand
I think Mark's code exhibits a bug: (def my-ref (ref 1)) (dosync (ref-set my-ref 5) (commute my-ref inc)) ; returns 6 (println "my-ref =" @my-ref) ; prints 7 since my-ref is ref-set, my-ref is in LockingTransaction/sets but its current value in LockingTransaction/vals is se

Re: commute documentation detail

2009-08-02 Thread Rich Hickey
On Sun, Aug 2, 2009 at 8:54 PM, Mark Volkmann wrote: > > On Sun, Aug 2, 2009 at 5:45 PM, Rich Hickey wrote: >> >> On Sun, Aug 2, 2009 at 3:20 PM, Mark Volkmann >> wrote: >>> >>> The doc for commute says "At the commit point of the transaction, sets

Re: commute documentation detail

2009-08-02 Thread Mark Volkmann
On Sun, Aug 2, 2009 at 5:45 PM, Rich Hickey wrote: > > On Sun, Aug 2, 2009 at 3:20 PM, Mark Volkmann > wrote: >> >> The doc for commute says "At the commit point of the transaction, sets >> the value of ref to be: >> (apply fun most-recently-committed-val

Re: commute documentation detail

2009-08-02 Thread Rich Hickey
On Sun, Aug 2, 2009 at 3:20 PM, Mark Volkmann wrote: > > The doc for commute says "At the commit point of the transaction, sets > the value of ref to be: > (apply fun most-recently-committed-value-of-ref args)". > > Looking at the source code in LockingTransaction.

Re: commute documentation detail

2009-08-02 Thread Mark Volkmann
I've been thinking about this more. Suppose I do something like this. (def my-ref (ref 1)) (defn f1 [] (dosync (ref-set my-ref 10))) (defn f2 [] (dosync (ref-set my-ref 5) (commute my-ref #(inc % (let [t1 (Thread. f1) t2 (Thread. f2)] (.start t1) (.sta

commute documentation detail

2009-08-02 Thread Mark Volkmann
The doc for commute says "At the commit point of the transaction, sets the value of ref to be: (apply fun most-recently-committed-value-of-ref args)". Looking at the source code in LockingTransaction.java, that doesn't seem to always be the case. I see this: f.fn.applyTo(RT.co

Re: Restarting a transaction with commute?

2009-02-01 Thread Rich Hickey
On Feb 1, 2009, at 6:47 AM, Paul Barry wrote: > My understanding of commute is that it would not restart the > transaction, it would just apply the function. So I wrote this > little test program: > > (defmacro in-thread [& body] > `(.start > (Thread. >

Restarting a transaction with commute?

2009-02-01 Thread Paul Barry
My understanding of commute is that it would not restart the transaction, it would just apply the function. So I wrote this little test program: (defmacro in-thread [& body] `(.start (Thread. (fn [] (println "Thread" (.getId (Thread/currentThread)) " started

Re: question about (commute messages conj msg)

2008-12-16 Thread Timothy Pratley
> and perl has closure, so it looks commute uses closure concept > > > My question is that conj takes two argument and how conj finds > > > the first argument? Is it somehow provided by commute? commute is passed conj as a function. commute then calls conj messages msg, an

Re: question about (commute messages conj msg)

2008-12-16 Thread wubbie
Thanks Randall. My background is mostly in imperative languages, including perl and perl has closure, so it looks commute uses closure concept to implement this. Is it right? Thanks again, sun On Dec 16, 9:28 pm, Randall R Schulz wrote: > On Tuesday 16 December 2008 18:10, wubbie wr

Re: question about (commute messages conj msg)

2008-12-16 Thread Randall R Schulz
On Tuesday 16 December 2008 18:10, wubbie wrote: > Hello, > > My question is that conj takes two argument and how conj finds > the first argument? Is it somehow provided by commute? Consider the documentation for (commute ...) (at <http://

question about (commute messages conj msg)

2008-12-16 Thread wubbie
Hello, My question is that conj takes two argument and how conj finds the first argument? Is it somehow provided by commute? Thanks, sun --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group.

Re: commute

2008-11-26 Thread Rich Hickey
On Nov 26, 2:35 pm, "Mark Volkmann" <[EMAIL PROTECTED]> wrote: > On Wed, Nov 26, 2008 at 1:13 PM, Shawn Hoover <[EMAIL PROTECTED]> wrote: > > > On Wed, Nov 26, 2008 at 1:55 PM, Mark Volkmann <[EMAIL PROTECTED]> > > wrote: > > >> The doc

Re: commute

2008-11-26 Thread Mark Volkmann
On Wed, Nov 26, 2008 at 1:13 PM, Shawn Hoover <[EMAIL PROTECTED]> wrote: > > > On Wed, Nov 26, 2008 at 1:55 PM, Mark Volkmann <[EMAIL PROTECTED]> > wrote: >> >> The documentation for commute says "Sets the in-transaction-value of >> ref ...". Th

Re: commute

2008-11-26 Thread Shawn Hoover
On Wed, Nov 26, 2008 at 1:55 PM, Mark Volkmann <[EMAIL PROTECTED]>wrote: > > The documentation for commute says "Sets the in-transaction-value of > ref ...". This implies to me that when the transaction ends, the ref > will have its previous value. > > (def

commute

2008-11-26 Thread Mark Volkmann
The documentation for commute says "Sets the in-transaction-value of ref ...". This implies to me that when the transaction ends, the ref will have its previous value. (def myRef (ref 19)) (dosync (commute myRef inc)) -> 20 @myRef -> 20 Why isn't the value of the last

Re: organizing multiple dosyncs and ref-set vs. commute

2008-08-25 Thread Rich Hickey
On Aug 25, 3:33 pm, "Shawn Hoover" <[EMAIL PROTECTED]> wrote: > On Mon, Aug 25, 2008 at 11:57 AM, Rich Hickey <[EMAIL PROTECTED]> wrote: > > > (def hits (ref 0)) > > (def cache (ref {:n nil :factors nil})) > > (def cache-hits (ref 0)) > > &

Re: organizing multiple dosyncs and ref-set vs. commute

2008-08-25 Thread Shawn Hoover
On Mon, Aug 25, 2008 at 11:57 AM, Rich Hickey <[EMAIL PROTECTED]> wrote: > > (def hits (ref 0)) > (def cache (ref {:n nil :factors nil})) > (def cache-hits (ref 0)) > > (defn cached-factor [n] > (dosync (commute hits inc)) > (let [cached @cache] >(if

Re: organizing multiple dosyncs and ref-set vs. commute

2008-08-25 Thread Rich Hickey
se(resp, factors); > } > > } > > And in Clojure (stripping out the Servlet noise): > > (def *last-number* (ref nil)) > (def *last-factors* (ref nil)) > (def *count* (ref 0)) > (def *cache-hits* (ref 0)) > > (defn cached-factor [n] >(dosyn