Skip to content

Instantly share code, notes, and snippets.

View vvvvalvalval's full-sized avatar

Valentin Waeselynck vvvvalvalval

View GitHub Profile
@vvvvalvalval
vvvvalvalval / gist:c6c2d991bdc96cce4c14
Last active February 11, 2024 19:34
Asynchronous map function with clojure.core.async
(require '[clojure.core.async :as a])
(defn- seq-of-chan "Creates a lazy seq from a core.async channel." [c]
(lazy-seq
(let [fst (a/<!! c)]
(if (nil? fst) nil (cons fst (seq-of-chan c)) ))))
(defn map-pipeline-async "Map for asynchronous functions, backed by clojure.core.async/pipeline-async .
From an asynchronous function af, and a seq coll, creates a lazy seq that is the result of applying the asynchronous function af to each element of coll.
@vvvvalvalval
vvvvalvalval / async_promise_listener_hack.clj
Last active August 29, 2015 14:10
Hack for attaching asynchronous callbacks to Clojure promises
(require '[clojure.core.async :as a])
(defn make-promise-listener!
"Creates a function that attaches handlers to Clojure promises.
Behind the scences, fires a loop in another logical thread that scans every refresh-period ms the promises that have been registered for completion,
and executes the each handler on the corresponding value."
[{:keys [refresh-period]
:or {refresh-period 10}}]
(let [next-index! (let [a (atom -1)] #(swap! a inc)) ;; to get unique keys to put in the map.
state (atom {})] ;; map of dummy unique keys to pending promise-handler pairs