Skip to content

Instantly share code, notes, and snippets.

@joshjones
joshjones / control-loop.clj
Created May 23, 2017 21:11
Methods to stop a loop from executing.
(defn ping-forever-core-async
[host ms-pause]
(let [termination-ch (chan)
termination-fn #(put! termination-ch :die)]
(go-loop [sleep-ch (timeout 0)]
(let [[_ ch] (alts! [sleep-ch termination-ch])]
(if (= ch sleep-ch)
(do
(println "Pinging" host)
(recur (timeout ms-pause)))
@joshjones
joshjones / spec-stub-function.clj
Created April 9, 2017 16:51
Stubbing a function using clojure spec
;;;;;;;;;;; STUBBING A FUNCTION
(defn select-id [id]
; real code would connect to db, just simulating here
{:col1 (rand-int 101)
:col2 (rand-nth ["Joe" "Bob" "Bill" "Sally"])})
(s/def ::col1 nat-int?)
(s/def ::col2 (s/with-gen string? #(gen/such-that (complement empty?) (gen/string-alphanumeric))))
(s/def ::db-row (s/keys :req-un [::col1 ::col2]))
@joshjones
joshjones / create-join-threads.clj
Last active May 23, 2021 19:55
Creating and joining threads in clojure
(import '[java.util.concurrent Callable
CountDownLatch
Executors
Semaphore
TimeUnit])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Examples of creating and joining threads in clojure
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(let [numthreads 5