-
-
Save MichaelDrogalis/4636837 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]) | |
; 10 | |
; 9 | |
; 8 | |
; 7 | |
; 6 | |
; 5 | |
; 4 | |
; 3 | |
; 2 | |
; 1 | |
; Done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment