Skip to content

Instantly share code, notes, and snippets.

@rwat
Created April 30, 2018 14:08
Show Gist options
  • Save rwat/a4c45411897d0698ea8f46d35d5a558d to your computer and use it in GitHub Desktop.
Save rwat/a4c45411897d0698ea8f46d35d5a558d to your computer and use it in GitHub Desktop.
(ns pg.util
(:require [clojure.core.async :refer [>! <! >!! <!! alts! alts!! chan go go-loop put! take! thread timeout close!] :as async]))
;; synchronous; takes from channel argument if something is available, else returns nil
(def maybe-take!
(let [closed-ch (doto (chan) (close!))]
(fn [ch]
(let [[x port] (alts!! [ch closed-ch] {:priority true})]
(when (= port ch)
x)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment