Advertisement
Guest User

Untitled

a guest
Sep 9th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns hello_seymore.core
  2.   (:require [sablono.core :as sab]))
  3.  
  4. ;(defonce app-state (atom { :likes 0 }))
  5. ;(def exp-stack (atom { :x (atom '()) :y (atom '()) :operator (atom '()) :result 0}))
  6. (def x (atom { :val '() }))
  7. (def y (atom { :val '() }))
  8. (def op (atom { :val '() }))
  9. (def result (atom { :val '() }))
  10.  
  11.  
  12.  
  13. (defn a-simple-stateful-object [x y op]
  14.   (sab/html [:div
  15.              ;[:h1 "Behold. The mighty counter. Clicked " (:likes @data) " times."]
  16.              ;[:div [:a {:href "#"
  17.              ;           :onClick #(swap! data update-in [:likes] inc)}
  18.              ;       "Click the counter."]
  19.              ; "    "
  20.              ;      [:a {:href "#"
  21.              ;           :onClick #(swap! data update-in [:likes] dec)}
  22.              ;       "Reverse the arrow of time."]]
  23.              ;[:p ""]
  24.              [:div
  25.               [:h2 "Let's make an adder!"]
  26.               [:div [:a {:href "#"
  27.                          :onClick ; just the basics, ma'am.
  28.                                   ; enclose an addition operator in the op atom
  29.                                    #(swap! op update-in [:val] (fn [arg] (quote + arg)))}
  30.                      "+"]
  31.                " "
  32.                     [:a {:href "#"
  33.                          :onClick  ; enclose a 1 in the x atom
  34.                                     #(swap! x update-in [:val] (fn [arg] (quote 1 arg)))}
  35.                                     ;(swap! (:y @registers) (fn [y] (apply str y 1))))}
  36.                       "1"]
  37.                " "
  38.                     [:a {:href "#"
  39.                          :onClick  ; enclose a 2 in the y atom
  40.                                    ;#(if (not (string? (:operator @registers)))
  41.                                     #(swap! y update-in [:val] (fn [arg] (quote 2 arg)))}
  42.                                     ;(swap! registers update-in [:y] str % 2)))}
  43.                      "2"]
  44.                 " "
  45.                   [:a {:href "#"
  46.                        :onClick ; add x and y
  47.                                #(swap! result update-in [:val] (fn [arg] (apply + (vector (:val @x) (:val @y))))) }
  48.                      "="]
  49.  
  50.               [:div
  51.                [:p
  52.                  [:h2 "Value of x: " (:val @x)]
  53.                  [:h2 "Value of y: " (:val @y)]
  54.                  [:h2 "  Operator: " (:val @op)]
  55.                  [:h2 "    Result: " (:val @result)]]]
  56.               ]]]
  57.  
  58.  
  59.             ))
  60.  
  61.  
  62.  
  63.  
  64. (defn render! []
  65.   (.render js/React
  66.            (a-simple-stateful-object x y op)
  67.            (.getElementById js/document "app")))
  68.  
  69.  
  70. (add-watch x :on-change (fn [_ _ _ _] (render!)))
  71.   (add-watch y :on-change (fn [_ _ _ _] (render!)))
  72.   (add-watch op :on-change (fn [_ _ _ _] (render!)))
  73. (add-watch result :on-change (fn [_ _ _ _ _ _ _ _ _] (render!)))
  74.  
  75. (render!)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement