Advertisement
Guest User

Untitled

a guest
Feb 25th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn snag-quotes [n]
  2.   (let [c (chan)
  3.         p (promise)]
  4.     ;; Reading n items from the channel here. The vec is delivered
  5.     ;; through a go-block result channel. There is also go-loop for
  6.     ;; this:
  7.     (go (loop [n n
  8.                acc []]
  9.           (if (zero? n)
  10.             (do
  11.               (show "x")
  12.               (deliver p acc))
  13.             (let [x (<! c)]
  14.               (show "!")
  15.               (recur (dec n)
  16.                      (conj acc x))))))
  17.     ;; Posting to the channel here, each from a separate "thread". An
  18.     ;; exception that prevents a put would deadlock the reader down
  19.     ;; there:
  20.     (dotimes [n n]
  21.       (go (>! c (try
  22.                   (show "+")
  23.                   (let [x (wget)]
  24.                     (show ".")
  25.                     x)
  26.                   (catch Exception e
  27.                     ;; (str (.getMessage e))
  28.                     e)))))
  29.     (deref p)))
  30.  
  31.  
  32. hello-world.core> (def x (time (snag-quotes 512)))
  33. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+.+.+.+.+.+.+.+.+.+.\
  34. +.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+\
  35. .+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.\
  36. +.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+\
  37. .+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.\
  38. +.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+\
  39. .+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+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