Skip to content

Instantly share code, notes, and snippets.

@lilrooness
Last active December 25, 2015 21:22
Show Gist options
  • Select an option

  • Save lilrooness/5f5bc7ea95c3eb9d8713 to your computer and use it in GitHub Desktop.

Select an option

Save lilrooness/5f5bc7ea95c3eb9d8713 to your computer and use it in GitHub Desktop.
(ns counter.core
(:require [clojure.browser.repl :as repl]))
;; (defonce conn
;; (repl/connect "http://localhost:9000/repl"))
(enable-console-print!)
(def ctr (atom {:count 0}))
(defn increment [state] (let [count (:count state)]
(assoc state :count (inc count))))
(defn decrement [state] (let [count (:count state)]
(assoc state :count (dec count))))
(defn mkbutton [text, callback]
(let [button (.createElement js/document "button")
body (.-body js/document)]
(set! (.-id button) text)
(set! (.-innerText button) text)
(set! (.-onclick button) callback)
(.appendChild body button)
))
(defn render []
(let [d (.getElementById js/document "display")]
(set! (.-innerText d) (:count @ctr))))
(let [body (.-body js/document)
display (.createElement js/document "h1")]
(set! (.-id display) "display")
(set! (.-innerText display) (:count @ctr))
(.appendChild body display))
(mkbutton "dec" (fn [e] ((swap! ctr decrement) (render))))
(mkbutton "inc" (fn [e] ((swap! ctr increment) (render))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment