FAQ
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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Search Discussions

  • Allen Johnson at Nov 10, 2011 at 1:57 am
    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 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+unsubscribe@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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Takahiro at Nov 10, 2011 at 2:04 am
    I see! Thank you.

    2011/11/10 Allen Johnson <akjohnson78@gmail.com>:
    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 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+unsubscribe@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+unsubscribe@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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Allen Johnson at Nov 10, 2011 at 2:11 am
    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 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 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+unsubscribe@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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Takahiro at Nov 10, 2011 at 2:15 am
    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 <akjohnson78@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 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 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+unsubscribe@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+unsubscribe@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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Meikel Brandmeyer (kotarak) at Nov 10, 2011 at 6:23 am
    Hi,

    an atom can be sufficient, but you need to drop to a lower level.

    (defn !paws
    "Like swap! but returns the old value of the atom."
    [a f & args]
    (loop []
    (let [old-value @a
    new-value (apply f old-value args)]
    (if (compare-and-set! a old-value new-value)
    old-value
    (recur)))))

    (peek (!paws atom-with-queue pop))

    However, my suspicion is, that you don't clearly divide the state handling
    from the program logic. The peek should happen in the update function you
    pass to swap!. If this is not side-effect free, it means that you have to
    coordinate with other identities and resources. Then a ref or an agent
    might be indeed a better fit than the atom.

    Sincerely
    Meikel

    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Takahiro at Nov 10, 2011 at 9:27 am
    Hi, Meikel

    I didn't know there is a lower level operation. Thanks.
    !paws is a tony name :)
    However, my suspicion is, that you don't clearly divide the state handling
    from the program logic. The peek should happen in the update function you
    pass to swap!. If this is not side-effect free, it means that you have to
    coordinate with other identities and resources. Then a ref or an agent might
    be indeed a better fit than the atom.
    The best way might be sealing stuff in poll! function and using
    polymorphism so that we can easily switch identity between ref and
    atom.

    (defprotocol Pollable
    (poll! [x]))

    (extend-protocol Pollable
    clojure.lang.Ref
    (poll! [r]
    (dosync
    (let [item (peek @r)]
    (alter r pop)
    item)))
    clojure.lang.Atom
    (poll! [a] (peek (!paws a pop))))

    Maybe overengineering.

    2011/11/10 Meikel Brandmeyer (kotarak) <mb@kotka.de>:
    Hi,

    an atom can be sufficient, but you need to drop to a lower level.

    (defn !paws
    "Like swap! but returns the old value of the atom."
    [a f & args]
    (loop []
    (let [old-value @a
    new-value (apply f old-value args)]
    (if (compare-and-set! a old-value new-value)
    old-value
    (recur)))))

    (peek (!paws atom-with-queue pop))

    However, my suspicion is, that you don't clearly divide the state handling
    from the program logic. The peek should happen in the update function you
    pass to swap!. If this is not side-effect free, it means that you have to
    coordinate with other identities and resources. Then a ref or an agent might
    be indeed a better fit than the atom.

    Sincerely
    Meikel

    --
    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+unsubscribe@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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupclojure @
categoriesclojure
postedNov 10, '11 at 1:46a
activeNov 10, '11 at 9:27a
posts7
users3
websiteclojure.org
irc#clojure

People

Translate

site design / logo © 2022 Grokbase