Instantly share code, notes, and snippets.

@Jared314 /async4.clj
Last active Dec 20, 2015

Embed
What would you like to do?
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