Created
September 12, 2014 12:29
-
-
Save mlapshin/400bcf539dde680f1de1 to your computer and use it in GitHub Desktop.
This file contains 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 clojure-async-example. | |
(:require [clojure.core.async :as async :refer [chan | |
go go-loop | |
<! >! >!! <!! | |
alts! alts!! | |
timeout | |
tap mult pub sub | |
close!]])) | |
(def max-n 100) | |
(defn run [] | |
(let [ch (chan) | |
num (rand-int max-n)] | |
;; riddler | |
(go-loop [step 0] | |
(let [guess (<! ch)] | |
(cond | |
(< guess num) (do | |
(>! ch :greater) | |
(println "riddler: the number is greater than" guess) | |
(recur (inc step))) | |
(> guess num) (do | |
(>! ch :less) | |
(println "riddler: the number is less than" guess) | |
(recur (inc step))) | |
:else (do | |
(>! ch :ok) | |
(println "riddler: BINGO! after" step "steps, bye!"))))) | |
;; guesser | |
(go-loop [guess (rand-int max-n)] | |
(>! ch guess) | |
(println "guesser:" guess) | |
(let [answer (<! ch)] | |
(case answer | |
:ok (println "guesser: bye!") | |
:less (recur (quot guess 2)) | |
:greater (recur (+ guess 1))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment