Created
January 25, 2013 18:48
-
-
Save XPherior/4636833 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 clotp.core | |
(:require [clojure.core.match :refer [match]])) | |
(def next-process-id (atom 0)) | |
(def processes (atom {})) | |
(defn next-pid [] | |
(swap! next-process-id inc)) | |
(defn spawn [f & args] | |
(let [pid (next-pid)] | |
(swap! processes #(assoc % pid (java.util.concurrent.LinkedBlockingQueue.))) | |
(future (apply f pid args)) | |
pid)) | |
(defn ! [pid message] | |
(.put (get @processes pid) message)) | |
(defn ? [pid] | |
(.take (get @processes pid))) | |
(defn counter [self] | |
(let [[n pid] (? self)] | |
(match [n] | |
[0] (println "Done!") | |
:else (do (println n) | |
(! pid [(dec n) self])))) | |
(recur self)) | |
(def a (spawn counter)) | |
(def b (spawn counter)) | |
(! a [10 b]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment