Skip to content

Instantly share code, notes, and snippets.

@Jared314
Last active December 20, 2015 01:19
Show Gist options
  • Save Jared314/6047789 to your computer and use it in GitHub Desktop.
Save Jared314/6047789 to your computer and use it in GitHub Desktop.
Clojure core.async example 4
(ns async4
(:require [clojure.core.async :as async]
[clojure.core.async.lab :as lab]))
(defn inmate [name c]
(async/go
(async/<! c)
(println name " released")))
(def alice (partial inmate "Alice"))
(def bob (partial inmate "Bob"))
(def carol (partial inmate "Carol"))
(def dave (partial inmate "Dave"))
(def eve (partial inmate "Eve"))
(defn judge-frank [& defendants]
(let [one-month (* 30 (* 24 (* 60 (* 60 1000)))) ; 1 month in msecs
sentence (fn [t] (lab/multiplex (async/timeout t)
(async/chan 1)))]
(for [d defendants
:let [t (* one-month (inc (rand-int 5)))]]
(d (sentence t)))))
(judge-frank
alice
bob
carol
dave
eve)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment