From d5741d38391cbca50a58365eeb0cf68df8848cbc Mon Sep 17 00:00:00 2001 From: hasufell Date: Fri, 14 Nov 2014 22:58:21 +0100 Subject: [PATCH] PARSER: use 'Either' from Prelude instead of custom QuadOrOrient --- Graphics/Diagram/Plotter.hs | 16 ++++++++-------- Parser/PathParser.hs | 29 +++++++++++------------------ 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/Graphics/Diagram/Plotter.hs b/Graphics/Diagram/Plotter.hs index 2693d06..7e49683 100644 --- a/Graphics/Diagram/Plotter.hs +++ b/Graphics/Diagram/Plotter.hs @@ -197,11 +197,11 @@ quadPathSquare = Diag f # moveTo (p2 ((xmax + xmin) / 2,(ymax + ymin) / 2)) # lw thin # lc red) (getSquare (stringToQuads (pQt p)) (qt, [])) where - getSquare :: [QuadOrOrient] -> Zipper PT -> Square + getSquare :: [Either Quad Orient] -> Zipper PT -> Square getSquare [] z = getSquareByZipper (dX p, dY p) z getSquare (q:qs) z = case q of - Orient x -> getSquare qs (fromMaybe z (findNeighbor x z)) - Quad x -> getSquare qs (fromMaybe z (goQuad x z)) + Right x -> getSquare qs (fromMaybe z (findNeighbor x z)) + Left x -> getSquare qs (fromMaybe z (goQuad x z)) qt :: QuadTree PT qt = quadTree vtf (dX p, dY p) vtf :: [PT] @@ -218,13 +218,13 @@ gifQuadPath = GifDiag f # moveTo (p2 ((xmax + xmin) / 2,(ymax + ymin) / 2)) # lw thick # lc col) <$> (getSquares (stringToQuads (pQt p)) (qt, [])) where - getSquares :: [QuadOrOrient] -> Zipper PT -> [Square] + getSquares :: [Either Quad Orient] -> Zipper PT -> [Square] getSquares [] z = [getSquareByZipper (dX p, dY p) z] getSquares (q:qs) z = case q of - Orient x -> getSquareByZipper (dX p, dY p) z : - getSquares qs (fromMaybe z (findNeighbor x z)) - Quad x -> getSquareByZipper (dX p, dY p) z : - getSquares qs (fromMaybe z (goQuad x z)) + Right x -> getSquareByZipper (dX p, dY p) z : + getSquares qs (fromMaybe z (findNeighbor x z)) + Left x -> getSquareByZipper (dX p, dY p) z : + getSquares qs (fromMaybe z (goQuad x z)) qt :: QuadTree PT qt = quadTree vtf (dX p, dY p) vtf :: [PT] diff --git a/Parser/PathParser.hs b/Parser/PathParser.hs index f844653..9986c98 100644 --- a/Parser/PathParser.hs +++ b/Parser/PathParser.hs @@ -7,16 +7,9 @@ import Parser.Core import Algorithms.RangeSearch.Core (Quad(NW, NE, SW, SE), Orient(North, South, West, East)) --- |Can either be an Orientation or a Quad, corresponding to the --- Algorithms.RangeSearch module. -data QuadOrOrient = Orient Orient - | Quad Quad - deriving (Show) - - -- |Parse a string such as "ne, n, sw, e" into -- [Quad NE, Orient North, Quad SW, Orient East]. -stringToQuads :: String -> [QuadOrOrient] +stringToQuads :: String -> [Either Quad Orient] stringToQuads str = case runParser parsePath str of Nothing -> [] Just xs -> fst xs @@ -28,19 +21,19 @@ stringToQuads str = case runParser parsePath str of -- |Parses a string that represents a single squad into the -- QuadOrOrient ADT. -parseQuad :: Parser QuadOrOrient +parseQuad :: Parser (Either Quad Orient) parseQuad = - const (Quad NW) <$> (string "nw" <|> string "NW") - <|> const (Quad NE) <$> (string "ne" <|> string "NE") - <|> const (Quad SW) <$> (string "sw" <|> string "SW") - <|> const (Quad SE) <$> (string "se" <|> string "SE") + const (Left NW) <$> (string "nw" <|> string "NW") + <|> const (Left NE) <$> (string "ne" <|> string "NE") + <|> const (Left SW) <$> (string "sw" <|> string "SW") + <|> const (Left SE) <$> (string "se" <|> string "SE") -- |Parses a string that represents a single Orientation into the -- QuadOrOrient ADT. -parseOrient :: Parser QuadOrOrient +parseOrient :: Parser (Either Quad Orient) parseOrient = - const (Orient North) <$> (string "n" <|> string "N") - <|> const (Orient South) <$> (string "s" <|> string "S") - <|> const (Orient West) <$> (string "w" <|> string "W") - <|> const (Orient East) <$> (string "e" <|> string "E") + const (Right North) <$> (string "n" <|> string "N") + <|> const (Right South) <$> (string "s" <|> string "S") + <|> const (Right West) <$> (string "w" <|> string "W") + <|> const (Right East) <$> (string "e" <|> string "E")