Re: atoms not working same in cljs as in clj?

2016-05-20 Thread Kenneth Tilton
On Fri, May 20, 2016 at 12:50 PM, Kevin Downey wrote: > This is a difference in the type function. The clojurescript type > function ignores metadata. Thanks! That was my second guess, but I looked and did not see that documented on the differences page. I already had an ia-type? function enca

Re: atoms not working same in cljs as in clj?

2016-05-20 Thread Kevin Downey
This is a difference in the type function. The clojurescript type function ignores metadata. So the clojurescript type function is like the clojure class function, and clojurescript doesn't have a class function. On 05/20/2016 09:22 AM, hiskennyness wrote: > I see here > https://github.com/clojure

Re: Atoms, reference to itself cause StackOverflowError

2015-08-07 Thread Simone Mosciatti
Thank you :) Extremely clear ! On Friday, August 7, 2015 at 3:48:14 PM UTC+2, Stuart Sierra wrote: > > Hi Simone, > > The stack overflow here is caused by the REPL trying to print a circular > reference. `swap!` always returns the new value of the Atom, and the REPL > tries to print it. > > If

Re: Atoms, reference to itself cause StackOverflowError

2015-08-07 Thread Stuart Sierra
Hi Simone, The stack overflow here is caused by the REPL trying to print a circular reference. `swap!` always returns the new value of the Atom, and the REPL tries to print it. If you don't print the Atom, this self-reference can still work: user=> (def a (atom {})) #'user/a user=> (do (swap!

Re: atoms, memoize, future-s and CAS

2014-12-20 Thread Andy L
Hi, Thanks for all suggestions. It all encouraged me to deep dive into atom-s code which turns out to be a simple wrapper over Java java.util.concurrent.atomic.AtomicReference which essentially is a spinlock. Knowing how it works under the hood makes so easier to use it ... Below piece (hopefully

Re: atoms, memoize, future-s and CAS

2014-12-08 Thread Michał Marczyk
On 8 December 2014 at 21:46, Fluid Dynamics wrote: > [...] > Which means it's locking or bust. You just get to either do the locking > yourself or delegate :) Sure, but isn't it nice when somebody else does your locking for you? :-) Incidentally, there is a trade-off here between lockless reads

Re: atoms, memoize, future-s and CAS

2014-12-08 Thread Michał Marczyk
Oh, and as for how to use it here, you could for example say (.putIfAbsent concurrent-hash-map :foo (delay (foo))) Then the first thread to @(get concurrent-hash-map :foo (delay :not-found)) (or similar) would actually compute the value. With a map in an atom, you could swap! using a function

Re: atoms, memoize, future-s and CAS

2014-12-08 Thread Fluid Dynamics
On Monday, December 8, 2014 3:34:05 PM UTC-5, Michał Marczyk wrote: > > On 8 December 2014 at 17:54, Andy L > > wrote: > >> But I'd personally just use a delay rather than "locking" for this > >> purpose. > > > > > > It is not that I like locking at all. However I still fail to see, how > in

Re: atoms, memoize, future-s and CAS

2014-12-08 Thread Michał Marczyk
On 8 December 2014 at 17:54, Andy L wrote: >> But I'd personally just use a delay rather than "locking" for this >> purpose. > > > It is not that I like locking at all. However I still fail to see, how in a > multithreaded context memoize/cache prevents executing a given function more > than once

Re: atoms, memoize, future-s and CAS

2014-12-08 Thread Andy L
> > > Most of the cache implementations in core.cache have no side-effects. They > simply return a new cache rather than overwriting the old one. The memoize > library places the cache in an atom, so it's guaranteed to change > atomically. > I tried to read the cache code (btw an excellent exerci

Re: atoms, memoize, future-s and CAS

2014-12-07 Thread James Reeves
On 7 December 2014 at 05:13, Andy L wrote: > > > >> The SoftCache uses a ConcurrentHashMap, but that caching option isn't >> used in core.memoize. Are you building a custom memoizer? >> >> > WRT ConcurrentHashMap, it was an incorrect conclusion on my part. In any > case, I fail to see "thread saf

Re: atoms, memoize, future-s and CAS

2014-12-06 Thread Andy L
or even better (using future themselves as a "marker" in the atom): (defmacro map-future-swap! [a k f] `(locking ~a (when (not (contains? @~a ~k)) (swap! ~a assoc ~k (future (swap! ~a assoc ~k (~f ~k ) ) ) -- You received this message because you are subscribed t

Re: atoms, memoize, future-s and CAS

2014-12-06 Thread Andy L
> The SoftCache uses a ConcurrentHashMap, but that caching option isn't used > in core.memoize. Are you building a custom memoizer? > > WRT ConcurrentHashMap, it was an incorrect conclusion on my part. In any case, I fail to see "thread safety" in the cache implementation, but again I could be wron

Re: atoms, memoize, future-s and CAS

2014-12-06 Thread James Reeves
On 7 December 2014 at 01:13, Andy L wrote: > > Thanks for looking into that. This indeed would solve a "semantics" > problem of memoize, as it returns a value now. However, it seems that > clojure.core.memoize, > or rather clojure.core.cache memoize is based of, is not thread safe. > > It uses C

Re: atoms, memoize, future-s and CAS

2014-12-06 Thread Andy L
> (defn memoized [f] > (comp deref (memoize (fn [& args] (delay (apply f args) > > Thanks for looking into that. This indeed would solve a "semantics" problem of memoize, as it returns a value now. However, it seems that clojure.core.memoize, or rather clojure.core.cache memoize is based of,

Re: atoms, memoize, future-s and CAS

2014-12-06 Thread James Reeves
It sounds like you want a delay. Delays are guaranteed to execute their body only once, so we can combine a delay with an atom: (defn memoized [f] (comp deref (memoize (fn [& args] (delay (apply f args) In theory that should produce a memoize that executes the function only once for each se

Re: Atoms/Concurrency - usage pattern

2010-05-17 Thread Jules
Thanks for your replies - Meikel and Alex, I considered using refs, but perhaps wrongly, came to the conclusion that they were for coordinating multiple changes atomically within an STM ? I figured that I only had one shared piece of state - current- state (input and output are both effectively th

Re: Atoms/Concurrency - usage pattern

2010-05-17 Thread ataggart
If you are not concerned about coordination, then why not just grab the value beforehand and use compare-and-set: (let [old @my-atom new (my-fn arg)] (compare-and-set! my-atom old new) (do-stuff old new)) On the other hand, if you are concerned about coordination between multiple thread

Re: Atoms/Concurrency - usage pattern

2010-05-17 Thread Meikel Brandmeyer
Hi, On Mon, May 17, 2010 at 08:49:35AM -0700, Jules wrote: > The problem occurs in stateful objects when you receive an input and > need to both modify your state and generate and deliver some output. > > I store my internal state in an atom and use swap! to update it, > passing in the input and

Re: Atoms and Watchers

2009-02-22 Thread Timothy Pratley
"if the new value differs from the current value, it calls notifyWatches." I noticed that the 'changed' bool has been removed in favor of only notifying when different. Should this logic also be applied to reset() which currently does not check for delta? Regards, Tim. --~--~-~--~

Re: Atoms and Watchers

2009-02-22 Thread Rich Hickey
On Feb 22, 2009, at 3:48 PM, Mark Volkmann wrote: > > I'm not sure, but I may have found a bug in Atom.java. The swap method > calls compareAndSet. After that, if the new value differs from the > current value, it calls notifyWatches. However, at that point > compareAndSet would have already cal

Re: Atoms

2008-12-05 Thread Chouser
On Fri, Dec 5, 2008 at 11:07 AM, Rich Hickey <[EMAIL PROTECTED]> wrote: > > I'm working on that. It has utility even outside traditional reactive > contexts, in moving the imperative part of your logic outside of your state > transformation function. I think it's a good model. > > Chouser recentl

Re: Atoms

2008-12-05 Thread Mark Engelberg
So, earlier, I asked how atoms differ from using commute on refs. It sounds like the answer is that if you use atoms in a larger transaction, then as soon as the atom set is encountered, it actually changes instantly, so if you rollback, and do the transaction again, it's already been set, and wi

Re: Atoms

2008-12-05 Thread Rich Hickey
On Fri, Dec 5, 2008 at 11:01 AM, Stuart Sierra <[EMAIL PROTECTED]>wrote: > > On Dec 4, 8:02 pm, Rich Hickey <[EMAIL PROTECTED]> wrote: > > I've added a new reference type - atom. > > Feedback welcome, > > A request, if it's possible: allow watchers to be set on atoms and > refs in addition to agen

Re: Atoms

2008-12-05 Thread Stuart Sierra
On Dec 4, 8:02 pm, Rich Hickey <[EMAIL PROTECTED]> wrote: > I've added a new reference type - atom. > Feedback welcome, A request, if it's possible: allow watchers to be set on atoms and refs in addition to agents. I'd like to experiment with "reactive" programming using the different transactio

Re: Atoms

2008-12-05 Thread Stuart Sierra
On Dec 4, 8:02 pm, Rich Hickey <[EMAIL PROTECTED]> wrote: > I've added a new reference type - atom. I like it; it greatly simplifies a common use for Refs. "Clojure. Sometimes you just need to mutate." "Clojure. Mutate safely." -Stuart Sierra --~--~-~--~~~---~--~-

Re: Atoms

2008-12-05 Thread Randall R Schulz
On Friday 05 December 2008 06:33, Rich Hickey wrote: > On Dec 5, 9:09 am, Randall R Schulz <[EMAIL PROTECTED]> wrote: > > ... > > > > Are you ever going to get out to the Silicon Valley area to give a > > talk? > > I hope to get a slot at Java One in SF this spring. Anything less pricey? Maybe y

Re: Atoms

2008-12-05 Thread Rich Hickey
On Dec 5, 9:09 am, Randall R Schulz <[EMAIL PROTECTED]> wrote: > On Friday 05 December 2008 05:24, Rich Hickey wrote: > > > On Dec 5, 5:51 am, bOR_ <[EMAIL PROTECTED]> wrote: > > > Are there any screencasts planned which will feature atoms? (I > > > found that the screencasts are an excellent wa

Re: Atoms

2008-12-05 Thread Randall R Schulz
On Friday 05 December 2008 05:24, Rich Hickey wrote: > On Dec 5, 5:51 am, bOR_ <[EMAIL PROTECTED]> wrote: > > Are there any screencasts planned which will feature atoms? (I > > found that the screencasts are an excellent way of learning > > clojure). > > Screencasts are generally a side-effect of

Re: Atoms

2008-12-05 Thread Rich Hickey
On Dec 5, 8:24 am, Rich Hickey <[EMAIL PROTECTED]> wrote: > On Dec 5, 5:51 am, bOR_ <[EMAIL PROTECTED]> wrote: > > > Are there any screencasts planned which will feature atoms? (I found > > that the screencasts are an excellent way of learning clojure). > > Screencasts are generally a side-effec

Re: Atoms

2008-12-05 Thread Rich Hickey
On Dec 5, 8:50 am, Julian Morrison <[EMAIL PROTECTED]> wrote: > It seems like a pure efficiency optimization - used alone it doesn't > change semantics from dosync and alter over one ref. > > It makes me feel wary. What if I changed my design and wanted to do > more in the same transaction? What

Re: Atoms

2008-12-05 Thread Julian Morrison
It seems like a pure efficiency optimization - used alone it doesn't change semantics from dosync and alter over one ref. It makes me feel wary. What if I changed my design and wanted to do more in the same transaction? What if I later wanted to call a function that uses it in the scope of a wide

Re: Atoms

2008-12-05 Thread Parth Malwankar
Thanks for taking to time for such a detailed explanation Rich. This makes things much clear. And thanks Chouser for the pictorial representation. Parth On Dec 5, 6:24 pm, Rich Hickey <[EMAIL PROTECTED]> wrote: > On Dec 5, 5:51 am, bOR_ <[EMAIL PROTECTED]> wrote: > > > Are there any screencasts

Re: Atoms

2008-12-05 Thread Rich Hickey
On Dec 5, 5:51 am, bOR_ <[EMAIL PROTECTED]> wrote: > Are there any screencasts planned which will feature atoms? (I found > that the screencasts are an excellent way of learning clojure). Screencasts are generally a side-effect of a speaking engagement. I imagine next time I give a talk, I'll t

Re: Atoms

2008-12-05 Thread Parth Malwankar
On Dec 5, 6:02 am, Rich Hickey <[EMAIL PROTECTED]> wrote: > I've added a new reference type - atom. > > Docs here: > > http://clojure.org/atoms > > Feedback welcome, > > Rich Are the following equivalent or is one recommended over the other? The first (using atoms) is definitely more convenient

Re: Atoms

2008-12-05 Thread bOR_
Are there any screencasts planned which will feature atoms? (I found that the screencasts are an excellent way of learning clojure). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this gr

Re: Atoms

2008-12-04 Thread Krukow
On Dec 5, 7:51 am, Krukow <[EMAIL PROTECTED]> wrote: > Looks useful as a kind of high-level interface to > java.util.concurrent.AtomicReference. Am I correct to think of this as being (semantically) equivalent to combining send- off and await with agents? E.g., (defn memoize [f] (let [mem (a

Re: Atoms

2008-12-04 Thread Krukow
On Dec 5, 2:02 am, Rich Hickey <[EMAIL PROTECTED]> wrote: > I've added a new reference type - atom. Looks useful as a kind of high-level interface to java.util.concurrent.AtomicReference. Am I correct --~--~-~--~~~---~--~~ You received this message because you are

Re: Atoms

2008-12-04 Thread Larrytheliquid
One difference would be if a ref is already inside of a bigger transaction that failed to commit for other reasons. With atoms it seems like the "transaction" is implicitly isolated to the atom (instead of explicitly wrapping around a ref.) On Thu, Dec 4, 2008 at 9:26 PM, Mark Engelberg <[EMAIL PR

Re: Atoms

2008-12-04 Thread Mark Engelberg
Didn't commute essentially give this behavior for refs? How is this different? On Thu, Dec 4, 2008 at 5:02 PM, Rich Hickey <[EMAIL PROTECTED]> wrote: > > I've added a new reference type - atom. > > --~--~-~--~~~---~--~~ You received this message because you are s