Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns hello_seymore.core
- (:require [sablono.core :as sab]))
- ; the following code works for basic arithmetic operations. It takes input from simple HTML buttons.
- ; 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
- ; themselves. It seems like the most common way to do this involves initializing an "io stream" -- implemented by
- ; calling the render! function within a loop, attaching the event listener to a js/canvas element, and updating the
- ; atomic value of the keypress on call.
- ; is there some way just to add a watch for :on-key-down and update the keyboard-input atom on demand?
- ;TODO: change atomic value types from strings to string vectors, and cons new values to the vec
- ;TODO: implement 'undo' button to rewind app-state to previous state
- (def app-state (atom {:values {:x "" :y "" :op "" :result "" :keyboard-input "*crickets*"}
- :current-target :x))
- (def keyboard-input (atom {:key-id nil}))
- (defn flushRegister [m]
- ; Destructively clear the associated register.
- (swap! app-state assoc-in [:values m] ""))
- (defn show [arg]
- ; Return the current value of the associated register.
- (cond (= arg 'x) (get-in @app-state [:values :x])
- (= arg 'y) (get-in @app-state [:values :y])
- (= arg 'op) (get-in @app-state [:values :op])
- (= arg 'result) (get-in @app-state [:values :result])
- (= arg 'current-target) (get-in @app-state [:current-target])
- ))
- (defn reval! [arg]
- ; Destructively update the value of the result register
- (let [current-op (show 'op)
- x-target (if (= (show 'x) "")
- :result
- :x)
- toggle-float (if (get-in @app-state [:x-is-a-float])
- (swap! app-state assoc-in [:values x-target] (float (show 'x))))
- xyvector (into [] (map js/parseFloat (into [] [(get-in @app-state [:values x-target])
- (get-in @app-state [:values :y])])))]
- (cond
- ; TODO: change dispatch type from string to keyword
- (= current-op "add")
- ; TODO; consolidate arithmetic procedures as single method that takes an operation argument
- (do
- (swap! app-state assoc-in [:values :result] (str (float (apply + xyvector))))
- (swap! app-state assoc-in [:values :x] (str (float (apply + xyvector))))
- (swap! app-state assoc-in [:values :y] "")
- (swap! app-state assoc-in [:values :op] "")
- (swap! app-state assoc-in [:current-target] :x))
- (= current-op "subtract")
- (do
- (swap! app-state assoc-in [:values :result] (str (apply - xyvector)))
- (swap! app-state assoc-in [:values :x] "")
- (swap! app-state assoc-in [:values :y] "")
- (swap! app-state assoc-in [:values :op] "")
- (swap! app-state assoc-in [:current-target] :x))
- (= current-op "divide")
- (do (if (= (show 'y) 0)
- (js/Error (str "Can't divide " (show 'x) " by zero"))
- (do
- (swap! app-state assoc-in [:values :result] (str (apply / xyvector)))
- (swap! app-state assoc-in [:values :x] "")
- (swap! app-state assoc-in [:values :y] "")
- (swap! app-state assoc-in [:values :op] "")
- (swap! app-state assoc-in [:current-target] :x))))
- (= current-op "multiply")
- (do
- (swap! app-state assoc-in [:values :result] (str (apply * xyvector)))
- (swap! app-state assoc-in [:values :x] "")
- (swap! app-state assoc-in [:values :y] "")
- (swap! app-state assoc-in [:values :op] "")
- (swap! app-state assoc-in [:current-target] :x))
- ))))
- (defn concatToRegister [atmap loc value]
- (let [pending-op (get-in @app-state [:values :op])
- current-target (get-in @app-state [:current-target])]
- (swap! atmap update-in [:values loc] str value)))
- (defn register-empty? [target]
- (= (get-in @app-state [:values target]) ""))
- (defn op-button-handler [op]
- (cond (and (register-empty? :y) (register-empty? :op)) (do
- (swap! app-state assoc-in [:values :op] op)
- (swap! app-state assoc-in [:current-target] :y))
- (and (not (register-empty? :y)) (not (register-empty? :op))) (do
- (reval! 'equals)
- (swap! app-state assoc-in [:values :x] (show 'result))
- (swap! app-state assoc-in [:values :op] op)
- (swap! app-state assoc-in [:current-target] :y))))
- (defn a-simple-stateful-object [app-state input]
- (let [target-register (get-in @app-state [:current-target])]
- (sab/html [:div
- [:button
- [:on-key-down #(fn [keyboard-input] (concatToRegister app-state target-register keyboard-input))]
- ]
- [:div
- [:button {:href "#"
- :onClick #(concatToRegister app-state target-register 1)}
- "1"] " "
- [:button {:href "#"
- :onClick #(concatToRegister app-state target-register 2)}
- "2"] " "
- [:button {:href "#"
- :onClick #(concatToRegister app-state target-register 3)}
- "3"] " "
- [:button {:href "#"
- :onClick #(op-button-handler "add")}
- "+"] " "
- [:button {:href "#"
- :onClick #(op-button-handler "divide")}
- "/"] " "]
- [:div
- [:button {:href "#"
- :onClick #(concatToRegister app-state target-register 4)}
- "4"] " "
- [:button {:href "#"
- :onClick #(concatToRegister app-state target-register 5)}
- "5"] " "
- [:button {:href "#"
- :onClick #(concatToRegister app-state target-register 6)}
- "6"] " "
- [:button {:href "#"
- :onClick #(op-button-handler "subtract")}
- "-"] " "
- [:button {:href "#"
- :onClick #(do
- (concatToRegister app-state target-register '.)
- )}
- "."] " "]
- [:div
- [:button {:href "#"
- :onClick #(concatToRegister app-state target-register 7)}
- "7"] " "
- [:button {:href "#"
- :onClick #(concatToRegister app-state target-register 8)}
- "8"] " "
- [:button {:href "#"
- :onClick #(concatToRegister app-state target-register 9)}
- "9"] " "
- [:button {:href "#"
- :onClick #(op-button-handler "multiply")
- }
- "*"] " "
- [:button {:href "#"
- :onClick #(do (reval! 'equals)
- (swap! app-state assoc-in [:values :x] "")
- (swap! app-state assoc-in [:values :op] ""))
- }
- "="] " "
- ]
- [:div
- [:h2 "Value of x: " (show 'x)]
- [:h2 "Value of y: " (show 'y)]
- [:h2 "Value of op:" (show 'op)]
- [:h2 "Value of result:" (show 'result)]
- [:h2 "Current target register:" (show 'current-target)]
- [:h2 "Keyboard says: " (get-in @keyboard-input [:key-id])]
- ]]
- )
- )
- )
- (defn render! []
- (.render js/React
- (a-simple-stateful-object app-state keyboard-input)
- (.getElementById js/document "app")))
- (add-watch keyboard-input :on-key-down (fn [_ _ _ _] (render!))) ;nope. this does not work.
- (add-watch app-state :on-change (fn [_ _ _ _] (render!)))
- (render!)
Advertisement
Add Comment
Please, Sign In to add comment