Guest User

Untitled

a guest
Sep 21st, 2015
120
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. ; the following code works for basic arithmetic operations. It takes input from simple HTML buttons.
  5. ; I'd like to extend the UI to also accept keyboard inputs. I can't find a way to attach event listeners to the buttons
  6. ; themselves. It seems like the most common way to do this involves initializing an "io stream" -- implemented by
  7. ; calling the render! function within a loop, attaching the event listener to a js/canvas element, and updating the
  8. ; atomic value of the keypress on call.
  9.  
  10. ; is there some way just to add a watch for :on-key-down and update the keyboard-input atom on demand?
  11.  
  12.  
  13. ;TODO: change atomic value types from strings to string vectors, and cons new values to the vec
  14. ;TODO: implement 'undo' button to rewind app-state to previous state
  15.  
  16. (def app-state (atom {:values {:x "" :y "" :op "" :result "" :keyboard-input "*crickets*"}
  17.                 :current-target :x))
  18.  
  19.  
  20.  
  21. (def keyboard-input (atom {:key-id nil}))
  22.  
  23.  (defn flushRegister [m]
  24.    ; Destructively clear the associated register.
  25.    (swap! app-state assoc-in [:values m] ""))
  26.  
  27.  
  28.  (defn show [arg]
  29.    ; Return the current value of the associated register.
  30.    (cond (= arg 'x) (get-in @app-state [:values :x])
  31.           (= arg 'y) (get-in @app-state [:values :y])
  32.           (= arg 'op) (get-in @app-state [:values :op])
  33.           (= arg 'result) (get-in @app-state [:values :result])
  34.           (= arg 'current-target) (get-in @app-state [:current-target])
  35.           ))
  36.  
  37.  
  38.  
  39.  (defn reval! [arg]
  40.    ; Destructively update the value of the result register
  41.    (let [current-op (show 'op)
  42.          x-target (if (= (show 'x) "")
  43.                     :result
  44.                     :x)
  45.          toggle-float (if (get-in @app-state [:x-is-a-float])
  46.                         (swap! app-state assoc-in [:values x-target] (float (show 'x))))
  47.          xyvector (into [] (map js/parseFloat (into [] [(get-in @app-state [:values x-target])
  48.                                                       (get-in @app-state [:values :y])])))]
  49.  
  50.      (cond
  51.        ; TODO: change dispatch type from string to keyword
  52.       (= current-op "add")
  53.                           ; TODO; consolidate arithmetic procedures as single method that takes an operation argument
  54.                               (do
  55.                                   (swap! app-state assoc-in [:values :result] (str (float (apply + xyvector))))
  56.                                   (swap! app-state assoc-in [:values :x] (str (float (apply + xyvector))))
  57.                                   (swap! app-state assoc-in [:values :y] "")
  58.                                   (swap! app-state assoc-in [:values :op] "")
  59.                                   (swap! app-state assoc-in [:current-target] :x))
  60.       (= current-op "subtract")
  61.                               (do
  62.                                   (swap! app-state assoc-in [:values :result] (str (apply - xyvector)))
  63.                                   (swap! app-state assoc-in [:values :x] "")
  64.                                   (swap! app-state assoc-in [:values :y] "")
  65.                                   (swap! app-state assoc-in [:values :op] "")
  66.                                   (swap! app-state assoc-in [:current-target] :x))
  67.       (= current-op "divide")
  68.                               (do (if (= (show 'y) 0)
  69.                                          (js/Error (str "Can't divide " (show 'x) " by zero"))
  70.                                          (do
  71.                                                (swap! app-state assoc-in [:values :result] (str (apply / xyvector)))
  72.                                                (swap! app-state assoc-in [:values :x] "")
  73.                                                (swap! app-state assoc-in [:values :y] "")
  74.                                                (swap! app-state assoc-in [:values :op] "")
  75.                                                (swap! app-state assoc-in [:current-target] :x))))
  76.       (= current-op "multiply")
  77.                               (do
  78.                                   (swap! app-state assoc-in [:values :result] (str (apply * xyvector)))
  79.                                   (swap! app-state assoc-in [:values :x] "")
  80.                                   (swap! app-state assoc-in [:values :y] "")
  81.                                   (swap! app-state assoc-in [:values :op] "")
  82.                                   (swap! app-state assoc-in [:current-target] :x))
  83.  
  84.      ))))
  85.  
  86.  
  87. (defn concatToRegister [atmap loc value]
  88.   (let [pending-op (get-in @app-state [:values :op])
  89.         current-target (get-in @app-state [:current-target])]
  90.  
  91.     (swap! atmap update-in [:values loc] str value)))
  92.  
  93. (defn register-empty? [target]
  94.   (= (get-in @app-state [:values target]) ""))
  95.  
  96.  
  97. (defn op-button-handler [op]
  98.   (cond (and (register-empty? :y) (register-empty? :op)) (do
  99.                                                            (swap! app-state assoc-in [:values :op] op)
  100.                                                            (swap! app-state assoc-in [:current-target] :y))
  101.         (and (not (register-empty? :y)) (not (register-empty? :op))) (do
  102.                                                                       (reval! 'equals)
  103.                                                                       (swap! app-state assoc-in [:values :x] (show 'result))
  104.                                                                       (swap! app-state assoc-in [:values :op] op)
  105.                                                                       (swap! app-state assoc-in [:current-target] :y))))
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  (defn a-simple-stateful-object [app-state input]
  112.    (let [target-register (get-in @app-state [:current-target])]
  113.   (sab/html [:div
  114.              [:button
  115.               [:on-key-down #(fn [keyboard-input] (concatToRegister app-state target-register keyboard-input))]
  116.               ]
  117.  
  118.              [:div
  119.  
  120.  
  121.               [:button {:href "#"
  122.                         :onClick #(concatToRegister app-state target-register 1)}
  123.                "1"] " "
  124.  
  125.               [:button {:href "#"
  126.                         :onClick #(concatToRegister app-state target-register 2)}
  127.                "2"] " "
  128.  
  129.               [:button {:href "#"
  130.                         :onClick #(concatToRegister app-state target-register 3)}
  131.                "3"] " "
  132.               [:button {:href "#"
  133.                         :onClick #(op-button-handler "add")}
  134.                "+"] " "
  135.               [:button {:href "#"
  136.                         :onClick #(op-button-handler "divide")}
  137.                "/"] " "]
  138.              [:div
  139.  
  140.  
  141.  
  142.               [:button {:href "#"
  143.                         :onClick #(concatToRegister app-state target-register 4)}
  144.                "4"] " "
  145.               [:button {:href "#"
  146.                         :onClick #(concatToRegister app-state target-register 5)}
  147.                "5"] " "
  148.               [:button {:href "#"
  149.                         :onClick #(concatToRegister app-state target-register 6)}
  150.                "6"] " "
  151.               [:button {:href "#"
  152.                         :onClick #(op-button-handler "subtract")}
  153.                "-"] " "
  154.              [:button {:href "#"
  155.                        :onClick #(do
  156.  
  157.                                   (concatToRegister app-state target-register '.)
  158.                                  )}
  159.                              
  160.               "."] " "]
  161.  
  162.              [:div
  163.  
  164.  
  165.               [:button {:href "#"
  166.                         :onClick #(concatToRegister app-state target-register 7)}
  167.                "7"] " "
  168.               [:button {:href "#"
  169.                         :onClick #(concatToRegister app-state target-register 8)}
  170.                "8"] " "
  171.               [:button {:href "#"
  172.                         :onClick #(concatToRegister app-state target-register 9)}
  173.                "9"] " "
  174.               [:button {:href "#"
  175.                         :onClick #(op-button-handler "multiply")
  176.                         }
  177.                "*"] " "
  178.               [:button {:href "#"
  179.                         :onClick #(do (reval! 'equals)
  180.                                       (swap! app-state assoc-in [:values :x] "")
  181.                                       (swap! app-state assoc-in [:values :op] ""))
  182.  
  183.                            }
  184.                "="] " "
  185.               ]
  186.  
  187.              [:div
  188.               [:h2 "Value of x: " (show 'x)]
  189.               [:h2 "Value of y: " (show 'y)]
  190.               [:h2 "Value of op:" (show 'op)]
  191.               [:h2 "Value of result:" (show 'result)]
  192.               [:h2 "Current target register:" (show 'current-target)]
  193.               [:h2 "Keyboard says: " (get-in @keyboard-input [:key-id])]
  194.              ]]
  195.  
  196.             )
  197.      )
  198.    )
  199.  
  200.  
  201. (defn render! []
  202.   (.render js/React
  203.            (a-simple-stateful-object app-state keyboard-input)
  204.            (.getElementById js/document "app")))
  205.  
  206.  
  207. (add-watch keyboard-input :on-key-down (fn [_ _ _ _] (render!))) ;nope. this does not work.
  208. (add-watch app-state :on-change (fn [_ _ _ _] (render!)))
  209. (render!)
Advertisement
Add Comment
Please, Sign In to add comment