Last active
December 20, 2015 01:19
-
-
Save Jared314/6047789 to your computer and use it in GitHub Desktop.
Clojure core.async example 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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