ncollins/tic-tac-monad — pull request #1 by rcjara (2013-04-16 15:32:57+00:00)
Title: Some tiny refactorings.

b3881af (19 hours) Add .gitignore

Hopefully self explanatory.

4a4ade8 (19 hours) Refactor gameState to eliminate unnecessary lambdas

- let rows = map (\l -> map (\s -> M.findWithDefault NoPiece s m) l) (boardRows board)
- columns = map (\l -> map (\s -> M.findWithDefault NoPiece s m) l) (boardColumns board)
- diagonals = map (\l -> map (\s -> M.findWithDefault NoPiece s m) l) (boardDiagonals board)
+ let findPiece = flip (M.findWithDefault NoPiece) m
+ rows = map (map findPiece) $ boardRows board
+ columns = map (map findPiece) $ boardColumns board
+ diagonals = map (map findPiece) $ boardDiagonals board
lines = rows ++ columns ++ diagonals
in
- if any (\line -> (all (\x -> x == X) line) || (all (\x -> x == O) line)) lines
+ if any (\line -> (all (== X) line) || (all (== O) line)) lines
then Victory
- else if all (\x -> x /= NoPiece) (concat rows)
+ else if all (/= NoPiece) (concat rows)
then Tie
else Continue

We discussed these changes yesterday.

2cb13ce (18 hours) Remove rows, cols, diagonals variables

let findPiece = flip (M.findWithDefault NoPiece) m
- rows = map (map findPiece) $ boardRows board
- columns = map (map findPiece) $ boardColumns board
- diagonals = map (map findPiece) $ boardDiagonals board
- lines = rows ++ columns ++ diagonals
+ lineCoords = concatMap (\f -> f board) [boardRows, boardColumns, boardDiagonals]
+ lines = map (map findPiece) lineCoords
in
if any (\line -> (all (== X) line) || (all (== O) line)) lines
then Victory
- else if all (/= NoPiece) (concat rows)
- then Tie
- else Continue
+ else if any (== NoPiece) (concat lines)
+ then Continue
+ else Tie

Here I wouldn't blame you if you felt I gave up clarity. I noticed the rows = / columns = / diagonals = lines were all a bit reduntant, so I changed them to a map. Then because I no longer had a rows variable I had to change the continue/tie checking. I elected to optimize the continue case, short cutting the check if any NoPiece showed up.

30d5161 (18 hours) Remove unused 'victory' function

--- i.e. it only checks if the most recent move has ended the game
-victory (Board size m) (row, column) piece =
- let row_coords = map (\y -> (row, y)) [1..size]
- col_coords = map (\x -> (x, column)) [1..size]
- squareEq p s = (M.findWithDefault NoPiece s m) == p
- in all (squareEq piece) row_coords || all (squareEq piece) col_coords

This totally wasn't being used.

3b0ffa6 (8 minutes) Give piece a custom show method. Remove an fmap call that turned pieces into strings.
-data Piece = X | O | NoPiece deriving (Eq, Show)
+data Piece = X | O | NoPiece deriving (Eq)
+
+instance Show Piece where
+ show X = "X"
+ show O = "O"
+ show _ = " "
+
showBoard :: Board -> String
-showBoard (Board size m) = let lookup sq = M.findWithDefault " " sq (fmap show m)
+showBoard (Board size m) = let lookup sq = show $ M.findWithDefault NoPiece sq m

We discussed this a little yesterday.

62f9120 (8 minutes) (HEAD, fork/master, master) Remove lambdas from boardRows / boardColumns

boardRows (Board size m) =
let range = [1..size]
- in map (\r -> map (\c -> (r,c)) range) range
+ in map (flip zip range . repeat) range

boardColumns (Board size m) =
let range = [1..size]
- in map (\c -> map (\r -> (r,c)) range) range
+ in map (zip range . repeat) range

These functions are basically the same, and I noticed that the inner map (in both functions) was really just a zip, so I replaced it to that, and also got to make use of that function composition period. Hooray!