about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-08-12T08·46+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-08-12T08·46+0100
commit17e1764ef8cfa7ebf6b8f1c6b4d4f6edd335e10d (patch)
treed137e4d1cbaa7b524c778f8107d1adee1047e459
parent4ff1ea291c266c68acfd662b7439d5c2061907ea (diff)
Generate coords function from existing qwerty keyboard
Per my take-home assignment's reviewer's comments, with which for the record I
agree, I should generate the character->coordinate table from my existing qwerty
keyboard definition.

The best part is: by doing this I found a bug: Notice how the original '0'
character was mapped to the Coordinate (0,0)... thus every subsequent digit
key (i.e. the first row) is off-by-one.
-rw-r--r--scratch/brilliant/Keyboard.hs52
1 files changed, 8 insertions, 44 deletions
diff --git a/scratch/brilliant/Keyboard.hs b/scratch/brilliant/Keyboard.hs
index ec3207079694..13b5de0145aa 100644
--- a/scratch/brilliant/Keyboard.hs
+++ b/scratch/brilliant/Keyboard.hs
@@ -5,6 +5,7 @@
 module Keyboard where
 --------------------------------------------------------------------------------
 import Utils
+import Data.Coerce
 import Data.Hashable (Hashable)
 import GHC.Generics (Generic)
 
@@ -32,50 +33,13 @@ instance Hashable Coord
 
 -- | List of characters to their QWERTY coordinatees.
 coords :: [(Char, Coord)]
-coords = [ ('0', Coord { row = 0, col = 0 })
-         , ('1', Coord { row = 0, col = 1 })
-         , ('2', Coord { row = 0, col = 2 })
-         , ('3', Coord { row = 0, col = 3 })
-         , ('4', Coord { row = 0, col = 4 })
-         , ('5', Coord { row = 0, col = 5 })
-         , ('6', Coord { row = 0, col = 6 })
-         , ('7', Coord { row = 0, col = 7 })
-         , ('8', Coord { row = 0, col = 8 })
-         , ('9', Coord { row = 0, col = 9 })
-         -- second row
-         , ('Q', Coord { row = 1, col = 0 })
-         , ('W', Coord { row = 1, col = 1 })
-         , ('E', Coord { row = 1, col = 2 })
-         , ('R', Coord { row = 1, col = 3 })
-         , ('T', Coord { row = 1, col = 4 })
-         , ('Y', Coord { row = 1, col = 5 })
-         , ('U', Coord { row = 1, col = 6 })
-         , ('I', Coord { row = 1, col = 7 })
-         , ('O', Coord { row = 1, col = 8 })
-         , ('P', Coord { row = 1, col = 9 })
-         -- third row
-         , ('A', Coord { row = 2, col = 0 })
-         , ('S', Coord { row = 2, col = 1 })
-         , ('D', Coord { row = 2, col = 2 })
-         , ('F', Coord { row = 2, col = 3 })
-         , ('G', Coord { row = 2, col = 4 })
-         , ('H', Coord { row = 2, col = 5 })
-         , ('J', Coord { row = 2, col = 6 })
-         , ('K', Coord { row = 2, col = 7 })
-         , ('L', Coord { row = 2, col = 8 })
-         , (';', Coord { row = 2, col = 9 })
-         -- fourth row
-         , ('Z', Coord { row = 3, col = 0 })
-         , ('X', Coord { row = 3, col = 1 })
-         , ('C', Coord { row = 3, col = 2 })
-         , ('V', Coord { row = 3, col = 3 })
-         , ('B', Coord { row = 3, col = 4 })
-         , ('N', Coord { row = 3, col = 5 })
-         , ('M', Coord { row = 3, col = 6 })
-         , (',', Coord { row = 3, col = 7 })
-         , ('.', Coord { row = 3, col = 8 })
-         , ('/', Coord { row = 3, col = 9 })
-         ]
+coords =
+  qwerty
+  |> coerce
+  |> fmap (zip [0..])
+  |> zip [0..]
+  |> fmap (\(row, xs) -> xs |> fmap (\(col, char) -> (char, Coord row col)))
+  |> mconcat
 
 -- | Mapping of characters to their coordinates on a QWERTY keyboard with the
 -- top-left corner as 0,0.