Skip to content

Instantly share code, notes, and snippets.

@rm-hull
Last active January 2, 2016 06:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rm-hull/8262502 to your computer and use it in GitHub Desktop.
Save rm-hull/8262502 to your computer and use it in GitHub Desktop.
Core.async example from Timothy Baldridge's Clojure/Conj 2013 talk: http://youtu.be/enwIIGzhahw, demonstrating 4800 'green' threads. Code modified from: https://github.com/halgari/clojure-conj-2013-core.async-examples/blob/master/src/clojure_conj_talk/core.clj#L585
(ns clojure-conj-talk.core
(:use [enchilada :only [canvas ctx canvas-size]]
[monet.canvas :only [fill-style fill-rect]]
[jayq.core :only [show]])
(:require [cljs.core.async :refer [<! >! chan timeout]])
(:require-macros [cljs.core.async.macros :as m :refer [go]]))
(def colors
(rand-nth [
[
"#E16889"
"#FE853E"
"#6EC59B"
"#FDBA52"
"#F5DED0"
"#94614C"
"#2D97D3"
"#48C3CB"
"#A9A6D3"
"#C0C1BC"
]
[
"#FFD1DC"
"#FFC0CB"
"#FFB7C5"
"#FC8EAC"
"#E75480"
"#DE5D83"
"#DE3163"
"#E30B5D"
"#E0115F"
"#C32148"
]]))
(defn make-cell [x y]
(go
(while true
(->
ctx
(fill-style (rand-nth colors))
(fill-rect {:x x :y y :w 9 :h 9}))
(<! (timeout (rand-int 1000))))))
(defn make-scene [rows cols]
(dotimes [x cols]
(dotimes [y rows]
(make-cell (* 10 x) (* 10 y)))))
(show canvas)
(let [[width height] (canvas-size)]
(make-scene
(/ height 10)
(/ width 10)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment