Skip to content

Commit

Permalink
freqs demo: removed all!/distinct!/timer* usage
Browse files Browse the repository at this point in the history
  • Loading branch information
alandipert committed Mar 17, 2013
1 parent 2585582 commit 79af51e
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/cljs/tailrecursion/javelin-demos/frequencies.cljs
Expand Up @@ -10,7 +10,7 @@
(:require-macros (:require-macros
[tailrecursion.javelin.macros :refer [cell]]) [tailrecursion.javelin.macros :refer [cell]])
(:require (:require
[tailrecursion.javelin :refer [all! distinct! timer*]] tailrecursion.javelin
[tailrecursion.javelin-demos.dom :refer [html! aset-in by-id form-cell]] [tailrecursion.javelin-demos.dom :refer [html! aset-in by-id form-cell]]
[tailrecursion.priority-map :refer [priority-map]])) [tailrecursion.priority-map :refer [priority-map]]))


Expand Down Expand Up @@ -63,9 +63,8 @@
slider (form-cell "#ms" :type :int, :default 500) slider (form-cell "#ms" :type :int, :default 500)
interval (cell (if (neg? slider) slider (- 1000 slider))) interval (cell (if (neg? slider) slider (- 1000 slider)))


;; data collection ;; stem cell
rand (all! (timer* interval (fn [_] (rand-int @rand-max)) (rand-int @rand-max))) freqs (cell '(priority-map (rand-int @rand-max) 1))
freqs (distinct! (cell (merge-with + ~(priority-map) {rand 1})))


;; analysis ;; analysis
n-seen (cell (->> freqs vals (reduce +))) n-seen (cell (->> freqs vals (reduce +)))
Expand All @@ -79,6 +78,11 @@
least-frequent (cell (key (peek freqs))) least-frequent (cell (key (peek freqs)))
n-distinct (cell (count freqs))] n-distinct (cell (count freqs))]


;; add random numbers to stem cell
((fn self []
(swap! freqs update-in [(rand-int @rand-max)] (fnil inc 1))
(.setTimeout js/window self @interval)))

;; display ;; display
(cell (html! "#status" "%s (interval is %s milliseconds)" (cell (html! "#status" "%s (interval is %s milliseconds)"
(if (neg? interval) "Stopped" "Running") (if (neg? interval) "Stopped" "Running")
Expand All @@ -91,4 +95,4 @@
(cell (histogram! "#histogram" freqs (cell (histogram! "#histogram" freqs
:bar-color "navy", :bar-color "navy",
:bar-width 20, :bar-width 20,
:bar-height 100)))) :bar-height 100))))

0 comments on commit 79af51e

Please sign in to comment.