From 8f5446a4144e46b2216a4f6c9064626956eb9f97 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Sun, 1 May 2016 05:40:30 -0400 Subject: [PATCH] Move parsing response entirely to Parser --- app/Main.hs | 2 +- src/Unused/Parser.hs | 16 +++++++++++----- src/Unused/Types.hs | 8 +------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 398ee85..598db32 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -9,7 +9,7 @@ main :: IO () main = do terms <- pure . lines =<< getContents results <- pure . concat =<< mapM search terms - let response = responseFromParse $ parseLines $ unlines results + let response = parseLines $ unlines results case withOneOccurrence $ withOneFile response of Right termMatchSet -> diff --git a/src/Unused/Parser.hs b/src/Unused/Parser.hs index a19ef1c..9288856 100644 --- a/src/Unused/Parser.hs +++ b/src/Unused/Parser.hs @@ -3,14 +3,20 @@ module Unused.Parser ) where import Control.Monad (void) -import Data.Map.Strict (fromList) -import Text.Parsec.String (Parser) +import Data.Bifunctor (second) import Text.Parsec -import Unused.Types +import Text.Parsec.String (Parser) +import qualified Data.Map.Strict as Map +import Unused.Util (groupBy) +import Unused.Types (TermMatch(..), ParseResponse, resultsFromMatches) -parseLines :: String -> Either ParseError [TermMatch] +parseLines :: String -> ParseResponse parseLines = - parse parseTermMatches "matches" + responseFromParse . parse parseTermMatches "matches" + +responseFromParse :: Either ParseError [TermMatch] -> ParseResponse +responseFromParse = + fmap $ Map.fromList . map (second resultsFromMatches) . groupBy term parseTermMatches :: Parser [TermMatch] parseTermMatches = do diff --git a/src/Unused/Types.hs b/src/Unused/Types.hs index 013f2e7..8bb6873 100644 --- a/src/Unused/Types.hs +++ b/src/Unused/Types.hs @@ -2,17 +2,15 @@ module Unused.Types ( TermMatch(..) , TermMatchSet , ParseResponse(..) - , responseFromParse , listFromMatchSet , withOneFile , withOneOccurrence + , resultsFromMatches ) where import Text.Parsec (ParseError) import Data.Bifunctor (second) import qualified Data.Map.Strict as Map -import Data.List (isInfixOf) -import Unused.Util (groupBy) data TermMatch = TermMatch { term :: String @@ -41,10 +39,6 @@ resultsFromMatches m = totalFiles' = length m totalOccurrences' = sum $ fmap occurrences m -responseFromParse :: Either ParseError [TermMatch] -> ParseResponse -responseFromParse = - fmap $ Map.fromList . map (second resultsFromMatches) . groupBy term - withOneFile :: ParseResponse -> ParseResponse withOneFile = fmap $ Map.filterWithKey (\_ a -> totalFiles a == 1)