Here are a few discussions about this issue:

http://dev.clojure.org/jira/browse/CLJ-1454 
<http://dev.clojure.org/jira/browse/CLJ-1454>
http://stackoverflow.com/questions/15441638/alternate-version-of-swap-also-returning-swapped-out-value
 
<http://stackoverflow.com/questions/15441638/alternate-version-of-swap-also-returning-swapped-out-value>
http://stackoverflow.com/questions/26838348/why-does-clojures-atom-swap-return-the-new-value
 
<http://stackoverflow.com/questions/26838348/why-does-clojures-atom-swap-return-the-new-value>

—Steve

> On Feb 1, 2016, at 1:05 PM, Jochen <joc...@riekhof.de> wrote:
> 
> Hi…
> 
> in my little project I do some concurrent stuff using atoms. 
> Now, during the last days using swap! I encountered the same problem several 
> times:
> Some result value is produced inside the swap! function besides the updated 
> data that I would like to return.
> I first played with two atoms and considered modeling the entire result as 
> the atom value and even using refs.
> The "fat atom" approach is ugly in this case as the enhanced result is just a 
> local artifact and not of interest outside my calls. Refs/STM looks somewhat 
> big for this.
> But then I saw compare-and-set! and came up with this:
> 
> (defn complex-swap! [atoom get-ref-val f & args]
>   (loop [last-val @atoom]
>     (let [result (apply f last-val args)]
>       (if (compare-and-set! atoom last-val (get-ref-val result))
>         result
>         (recur @atoom)))))
> 
> 
> It allows to produce an augmented result value, only part of which is the new 
> atom content passing an additional getter function to extract the new value 
> part.
> 
> For example:
> (def my-atom (atom '(1 2 3 4)))
> (complex-swap! my-atom second (partial split-with #(<= % 2)))
> => [(1 2) (3 4)]
> @my-atom
> => (3 4).
>  
> The original swap! would be like this:
> (swap! my-atom (comp second (partial split-with #(<= % 2))))
> => (3 4)
> @my-atom
> => (3 4)
> 
> Now, is there some even easier way to achieve this or is this the best 
> approach?
> 
> Ciao
> 
> …Jochen
> 
> 
> 
> -- 
> 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 your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en 
> <http://groups.google.com/group/clojure?hl=en>
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+unsubscr...@googlegroups.com 
> <mailto:clojure+unsubscr...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
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 your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to