Fetching contributors…
Cannot retrieve contributors at this time
40 lines (34 sloc) 1.05 KB
(ns co.paralleluniverse.pulsar.examples.pingpong
"The classic ping-pong example from the Erlang tutorial"
(:use [co.paralleluniverse.pulsar core actors])
(:refer-clojure :exclude [promise await]))
;; This example is intended to be a line-by-line translation of the canonical
;; Erlang [ping-pong example](,
;; so it is not written in idiomatic Clojure.
(defsfn ping [n pong]
(if (== n 0)
(! pong :finished)
(println "ping finished"))
(! pong [:ping @self])
:pong (println "Ping received pong"))
(recur (dec n) pong))))
(defsfn pong []
:finished (println "Pong finished")
[:ping ping] (do
(println "Pong received ping")
(! ping :pong)
(defn -main []
(let [a1 (spawn pong)
b1 (spawn ping 3 a1)]
#_(defn -main []
(let [a1 (spawn pong)
b1 (spawn ping 3 a1)]
(join a1)
(join b1)
(System/exit 0)))