It's also doable with just swap!, fwiw:

(defn make-blah [xs]
  (let [a (atom [nil xs])]
    (fn []
      (first (swap! a
                    (fn [[_ tail]]
                      [(first tail) (next tail)]))))))
Dave



On Thu, Dec 5, 2013 at 12:43 PM, Mark Engelberg <mark.engelb...@gmail.com>wrote:

> Although I think that a ref is the right tool for this purpose, for the
> sake of completeness, I'll illustrate the threadsafe way to use an atom
> here:
>
> (defn make-blah [v]
>   (let [a (atom v)]
>     (fn [] (let [current-val @a]
>              (if (compare-and-set! a current-val (rest current-val))
>                (first current-val)
>                (recur))))))
>
>
>
>
>
> On Thu, Dec 5, 2013 at 12:35 PM, Mark Engelberg 
> <mark.engelb...@gmail.com>wrote:
>
>> It's worth pointing out that if you really care about using it across
>> multiple threads, an atom really isn't the right tool for the job.  You
>> should use a ref in order to place both the first and the rest into a
>> single transaction:
>>
>> (defn make-blah [v]
>>   (let [a (ref v)]
>>     (fn [] (dosync (let [x (first @a)] (alter a rest) x)))))
>>
>>
>>
>> On Thu, Dec 5, 2013 at 12:24 PM, David Simmons 
>> <shortlypor...@gmail.com>wrote:
>>
>>> Hi Puzzler
>>>>
>>>
>>> I like the first approach you defined and it works perfectly thank you.
>>>
>>> cheers
>>>
>>> Dave
>>>
>>> --
>>> --
>>> 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/groups/opt_out.
>>>
>>
>>
>  --
> --
> 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/groups/opt_out.
>

-- 
-- 
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/groups/opt_out.

Reply via email to