Skip to content

Instantly share code, notes, and snippets.

@noisesmith
Last active February 25, 2018 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noisesmith/335606e11056ae6aca0fe983e92d6bf9 to your computer and use it in GitHub Desktop.
Save noisesmith/335606e11056ae6aca0fe983e92d6bf9 to your computer and use it in GitHub Desktop.
simple usage of halt-when with clojure core.async
+user=> (def c (>/chan 1 (halt-when even?)))
#'user/c
+user=> (>/take! c println)
nil
+user=> (>/put! c 1)
true1
+user=> (>/take! c println)
nil
+user=> (>/put! c 3)
true
user=> 3
+user=> (>/take! c println)
nil
+user=> (>/put! c 2)
true
nil
+user=> (>/take! c println)
nil
nil
+user=> (def c (>/chan 1 (halt-when even? (fn [x y] (println "halted on" x "with" y)))))
#'user/c
+user=> (>/take! c (fn [x] (println \newline "x:" x)))
nil
+user=> (>/put! c 1)
true
user=>
x: 1
+user=> (>/put! c 3)
true
+user=> (>/take! c (fn [x] (println \newline "x:" x)))
x: 3
nil
+user=> (>/take! c (fn [x] (println \newline "x:" x)))
nil
+user=> (>/put! c 2)
halted on #object[clojure.core.async.impl.buffers.FixedBuffer 0x2b9bbaa8 clojure.core.async.impl.buffers.FixedBuffer@2b9bbaa8] with 2
true
user=>
x: nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment