Skip to content

Instantly share code, notes, and snippets.

@threepointone
Last active March 22, 2016 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save threepointone/eee1972ad47c4a10b3a0 to your computer and use it in GitHub Desktop.
Save threepointone/eee1972ad47c4a10b3a0 to your computer and use it in GitHub Desktop.
an implementation of rob pike's parallel search slide in oia
oia(lets [go chan put take timeout alts] (require 'js-csp/lib/csp') (do
(fn fake [kind]
(fn [c query]
(go (gen []
(yield (take (timeout (js Math.random()*200))))
(yield (put c [kind query]))))))
(def web1 (fake :web1))
(def web2 (fake :web2))
(def image1 (fake :image1))
(def image2 (fake :image2))
(def video1 (fake :video1))
(def video2 (fake :video2))
(fn fastest [query replicas]
(let [c (chan)]
(.forEach replicas (fn [replica] (replica c query))) c))
(fn google [query]
(let [c (chan) t (timeout 100) sink (chan)]
(go (gen []
(yield (put c (yield (take (fastest query [$ web1 web2])))))))
(go (gen []
(yield (put c (yield (take (fastest query [$ image1 image2])))))))
(go (gen []
(yield (put c (yield (take (fastest query [$ video1 video2])))))))
(go (gen []
(def res [$])
(dotimes 3
(.push res (get (yield (alts [$ c t])) :value 'timed out')))
(yield (put sink res))))
sink))
(go (gen []
(prn (yield (take (google 'oia'))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment