Pipe dreams aren’t necessarily made of promises

unsorted — cgrand, 2 April 2010 @ 14 h 31 min

Because of the spinning nature of atoms, it’s kind of a hack (a fun hack but still a hack) to build queues on it. Here is the same pipe function built on Java queues:

(defn pipe []
  (let [q (java.util.concurrent.LinkedBlockingQueue.)
        EOQ (Object.)
        NIL (Object.)
        s (fn s [] (lazy-seq (let [x (.take q)]
                               (when-not (= EOQ x)
                                 (cons (when-not (= NIL x) x) (s))))))]
    [(s) (fn ([] (.put q EOQ)) ([x] (.put q (or x NIL))))]))

7 Comments »

  1. [...] follow-up: pipe dreams aren’t necessarily made of promises [...]

  2. Thanks again Christophe.

    Comment by Edmund — 2 April 2010 @ 16 h 10 min
  3. why did you decide to return a vector instead of a list?

    Comment by William Hidden — 2 April 2010 @ 16 h 38 min
  4. Nice. It took me a moment to understand the purpose of the NIL object, until I found that LinkedBlockingQueue.put won’t accept a null pointer.

    Comment by Stuart — 2 April 2010 @ 16 h 48 min
  5. J’y comprends toujours rien… mais la “spinning nature of atoms” ? Clojure sert à programmer des ordinateurs quantiques ?

    Comment by Olivier — 2 April 2010 @ 17 h 24 min
  6. @Olivier: un atome Clojure est une référence mutable synchrone non coordonnée (ne participe pas à une transaction) qui est basée sur une opération CAS. Le CAS pouvant échouer, la fonction de mise à jour peut être appliquée plusieurs fois : c’est ça la spinning nature of atoms ! Et oui en écrivant ceci je trouvais ça quantiquement drôle.

    Comment by cgrand — 3 April 2010 @ 20 h 08 min
  7. @William: In Clojure vectors are preferred over lists in such cases.

    Comment by Meikel — 8 April 2010 @ 10 h 03 min

RSS feed for comments on this post. TrackBack URI

Leave a comment

(c) 2010 Clojure and me | powered by WordPress with Barecity