Permalink
Browse files

Refactor to use maps for state.

  • Loading branch information...
1 parent 2407929 commit 44f21a2dffc54b958f5b30ce4dff2dc4be6bb810 @candera committed Mar 29, 2012
Showing with 9 additions and 30 deletions.
  1. +5 −22 src/clj/kchordr/core.clj
  2. +4 −8 test/kchordr/test/core.clj
@@ -1,5 +1,5 @@
(ns kchordr.core
- (:refer-clojure :exclude [key name]))
+ (:refer-clojure :exclude [key]))
(def ^{:private true
:doc "Map of keys to classes. Absence from this list means it's a normal key."}
@@ -16,27 +16,16 @@
(defn state
"Returns a new key-state object."
[]
- {:to-send []
- :state :ground})
+ {:to-send []})
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Key events
(defn event
"Given a key name and a direction, return a new key event."
- [name direction]
- [name direction])
-
-(defn key
- "Given a key event, return the key."
- [event]
- (first event))
-
-(defn direction
- "Given a key event, return the direction."
- [event]
- (second event))
+ [key direction]
+ {:key key :direction direction})
(defn append
"Append bs to a, where bs and a are (potentially empty) sequences of
@@ -108,8 +97,7 @@
(defn process
"Given the current state and a key event, return an updated state."
[state event]
- (let [key (key event)
- direction (direction event)
+ (let [{:keys [key direction]} event
cls (get key-classes key :normal)
keystate (:keystate state)]
(cond
@@ -127,8 +115,3 @@
:else
(assoc state :to-send (append (:to-send state) (event key direction))))))
-(defn to-send
- "Given a key state, return any pending key events, in the form of
- [key direction] pairs."
- [state]
- (:to-send state))
@@ -5,16 +5,12 @@
(defn- sent
"Given a sequence of key events, return the sequence of keys that
will actually be sent."
- [presses]
- (loop [state (state)
- press presses]
- (if (seq presses)
- (recur (process state press)
- (rest (rest presses)))
- (to-send state))))
+ [events]
+ (:to-send (reduce #(process %1 %2) (state) events)))
(deftest key-tests
- (are [pressed anticipated] (= anticipated (sent pressed))
+ (are [pressed anticipated]
+ (= anticipated (sent (map (partial apply event) pressed)))
;; Single non-modifier key press
[[:b :dn]]
[[:b :dn]]

0 comments on commit 44f21a2

Please sign in to comment.