Skip to content

Instantly share code, notes, and snippets.

@jmglov
Created July 6, 2016 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmglov/8b89fe00aefe3f94f3b904eae9db32c1 to your computer and use it in GitHub Desktop.
Save jmglov/8b89fe00aefe3f94f3b904eae9db32c1 to your computer and use it in GitHub Desktop.
Ping-pong game with core.async
(ns ping-pong
(:require [clojure.core.async :as async :refer [<!! >!!]]))
(defn ping [ch]
(println "Ping")
(>!! ch :ping)
(<!! (async/timeout 500)))
(defn pong [ch]
(println "Pong")
(>!! ch :pong)
(<!! (async/timeout 500)))
(defn return [ch]
(let [ball (<!! ch)]
(if (= ball :ping)
(pong ch)
(ping ch))))
(defn play []
(let [ch (async/chan 1)]
(loop [cnt 0]
(if (= cnt 0)
(ping ch)
(return ch))
(if (< cnt 10)
(recur (inc cnt))
(println "Game over!")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment