I see. You are right.
A function passed to swap! must return only poped value, so peeked
element from a queue is used only for side-effect in the function.

2011/11/10 Allen Johnson <akjohnso...@gmail.com>:
> Although now I see that this usage is breaking the "side-effect free"
> rule. So actually this is probably not what you want to do. :)
>
> Now I'm with you and thinking that atoms are not a proper fit for
> PersistentQueue. Maybe someone from the conj can shed some light on
> this.
>
> Allen
>
> On Wed, Nov 9, 2011 at 8:56 PM, Allen Johnson <akjohnso...@gmail.com> wrote:
>> In your example yes, but you could make it atomic by placing the
>> functionality in a function of it's own:
>>
>> ;; queue processing actions
>> (defn process-item [queue]
>>  (println "Processed item:" (peek queue))
>>  (pop queue))
>>
>> ;; usage
>> (let [queue (atom (into PersistentQueue/EMPTY [1 2 3]))]
>>  (swap! queue process-item) ; prints 1
>>  (swap! queue process-item) ; prints 2
>>  (swap! queue process-item)) ; prints 3
>>
>> Probably better examples but that's the basic idea.
>>
>> Allen
>>
>> On Wed, Nov 9, 2011 at 8:46 PM, Takahiro Hozumi <fat...@googlemail.com> 
>> wrote:
>>> Hi,
>>>
>>> To avoid peeking same element of a queue, I think PersistentQueue must
>>> be stored in ref not atom.
>>> Is this correct?
>>>
>>> ;;Ref: safe
>>> (dosync
>>>  (let [item (peek @r)]
>>>    (alter r pop)
>>>    item))
>>>
>>> ;Atom: unsafe
>>> (let [item (peek @a)]
>>>  (swap! a pop)
>>>  item))
>>>
>>> Thanks.
>>>
>>> --
>>> 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 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 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

Reply via email to