• Clojure
  • 40 lines
  • 72 views
  • 3 forks
  • Pasted by anonymous on September 10, 2013
(ns example.dice
  (:require
   [goog.events :as events]
   [goog.dom :as dom]
   [goog.object :as object]
   [goog.events.EventType :as EventType]
   [goog.ui.Component.EventType :as ComponentEventType]
   [goog.ui.Button :as Button]))

;; too much recursion
(defn simple-ouch []
  (first (filter #(= 9999 %) (range))))

;; works
(defn vector-ok []
  (first (filter #(= 9999 %) (vec (take 10000 (range))))))

;; works
(defn simple-ok []
  (some #(when (= 9999 %) %) (range)))

(defn bind-click-once [[fun fun-name]]
  (let [button-element (dom/createDom "div")
        button (goog.ui.Button. (format "(%s)" fun-name))]
    (.render button button-element)
    (events/listenOnce button ComponentEventType/ACTION
                       (fn []
                         (.setCaption button (str (fun)))))
    (dom/append (dom/getElement "controls") button-element)))

(defn ^:export bind []
  (mapv bind-click-once 
        [[simple-ouch "simple-ouch"]  [simple-ok "simple-ok"] [vector-ok "vector-ok"]] ))

;; you can use this html to run it:
;; <html><head><title>dice game</title></head>
;; <body><header id="controls"></header><script src="/js/main-debug.js" type="text/javascript"></script>
;; <script type="text/javascript">example.dice.bind()</script></body></html>


Did you know? CLOSE

  • There are keyboard shortcuts!
    • When Creating A Paste
      • ALT+P Toggle Private
      • CTRL+Enter Create Paste
      • ALT+W Toggle word wrap
    • When Viewing A Paste
      • ALT+G Go to a line
      • ALT+CTRL+E Edit the paste
      • ALT+R Show the raw code
  • There are URL options!
    • When Creating A Paste
      • ?lang=Javascript to default to javascript
    • When Viewing A Paste
      • #L-N Jump to line number N
?