view 6-Clojure/blockqingqueue.clj @ 93:39084e2b8744

Add a function for word-aware text wrapping Potentially hugely inefficient because we iterate through the string character by character, but then splitting it first and iterating over words still needs to iterate over the string to know where to split.
author IBBoard <dev@ibboard.co.uk>
date Tue, 18 Jun 2019 21:05:00 +0100
parents 920b50be0fe5
children
line wrap: on
line source

(defn create_blocking_queue [] (atom []))

(defn add_item [queue item]
    (swap! queue conj item))

(defn pop_item [queue]
    (future
        (
            (while (empty? @queue) (do
                (Thread/sleep 1000)
                (print ".")
            ))
            ;(@queue 0)
        )
    )
)

(def myqueue (create_blocking_queue))
; TODO: Add something after a period of time
(def updater (agent myqueue))
(send updater (fn [_q item] (Thread/sleep 5000) (add_item _q item) (println "Item added")) :item)
(println "Scheduled an item")
(def first_item (pop_item myqueue))
(println "Popped an item (future)")
(println first_item)
(println @first_item)