Skip to content

Instantly share code, notes, and snippets.

@MichaelDrogalis
Forked from XPherior/core.clj
Created January 25, 2013 18:49
Show Gist options
  • Save MichaelDrogalis/4636837 to your computer and use it in GitHub Desktop.
Save MichaelDrogalis/4636837 to your computer and use it in GitHub Desktop.
(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