Skip to content

Instantly share code, notes, and snippets.

@jmglov
Created July 6, 2016 10:57
Show Gist options
  • Save jmglov/d75787d31cf7c05fde09c45a01528ef2 to your computer and use it in GitHub Desktop.
Save jmglov/d75787d31cf7c05fde09c45a01528ef2 to your computer and use it in GitHub Desktop.
Non-blocking version of core.async ping-pong game
(ns ping-pong-go
(: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)]
(async/go-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