Skip to content

Commit 518ce7f

Browse files
committed
Add a Util module and move some functions out to it
1 parent 3ef1feb commit 518ce7f

File tree

6 files changed

+47
-41
lines changed

6 files changed

+47
-41
lines changed

hlint.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ executable hlint
4040
Report
4141
Type
4242
Test
43+
Util
4344
HSE.All
4445
HSE.Bracket
4546
HSE.Evaluate

src/CmdLine.hs

+1-26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import System.Exit
1111
import System.FilePath
1212
import HSE.All
1313

14+
import Util
1415
import Paths_hlint
1516
import Data.Version
1617

@@ -89,29 +90,3 @@ getFile file = do
8990
b <- doesFileExist file
9091
when (not b) $ error $ "Couldn't find file: " ++ file
9192
return [file]
92-
93-
94-
getDirectoryContentsRecursive :: FilePath -> IO [FilePath]
95-
getDirectoryContentsRecursive dir = do
96-
xs <- getDirectoryContents dir
97-
(dirs,files) <- partitionM doesDirectoryExist [dir </> x | x <- xs, not $ isBadDir x]
98-
rest <- concatMapM getDirectoryContentsRecursive dirs
99-
return $ files++rest
100-
101-
102-
partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a], [a])
103-
partitionM f [] = return ([], [])
104-
partitionM f (x:xs) = do
105-
res <- f x
106-
(as,bs) <- partitionM f xs
107-
return ([x|res]++as, [x|not res]++bs)
108-
109-
110-
concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
111-
concatMapM f = liftM concat . mapM f
112-
113-
114-
isBadDir :: FilePath -> Bool
115-
isBadDir x = "." `isPrefixOf` x || "_" `isPrefixOf` x
116-
117-

src/HSE/All.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import HSE.Evaluate
1515
import HSE.Bracket
1616
import HSE.Match
1717
import HSE.Operators
18-
18+
import Util
1919

2020

2121
-- | On failure returns an empty module and prints to the console

src/HSE/Util.hs

+1-13
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,7 @@ import Data.Generics.PlateData
77
import Data.List
88
import Data.Maybe
99
import Language.Haskell.Exts
10-
11-
12-
---------------------------------------------------------------------
13-
-- GENERAL FUNCTIONS
14-
15-
headDef :: a -> [a] -> a
16-
headDef x [] = x
17-
headDef x (y:ys) = y
18-
19-
20-
limit :: Int -> String -> String
21-
limit n s = if null post then s else pre ++ "..."
22-
where (pre,post) = splitAt n s
10+
import Util
2311

2412

2513
---------------------------------------------------------------------

src/Main.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Settings
1414
import Report
1515
import Type
1616
import Test
17+
import Util
1718
import HSE.All
1819
import Hint.All
1920
import Paths_hlint
@@ -25,7 +26,7 @@ main = do
2526
settings <- readSettings cmdHintFiles
2627
let extra = [Classify ("","") Ignore x | x <- cmdIgnore]
2728
let apply = map (classify $ settings ++ extra) . applyHint (readHints settings)
28-
ideas <- liftM concat $ mapM (liftM apply . parseFile) cmdFiles
29+
ideas <- concatMapM (liftM apply . parseFile) cmdFiles
2930
mapM_ print [i | i <- ideas, cmdShowAll || rank i /= Ignore]
3031

3132
-- figure out statistics

src/Util.hs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
module Util where
3+
4+
import Control.Monad
5+
import Data.List
6+
import System.Directory
7+
import System.FilePath
8+
9+
10+
getDirectoryContentsRecursive :: FilePath -> IO [FilePath]
11+
getDirectoryContentsRecursive dir = do
12+
xs <- getDirectoryContents dir
13+
(dirs,files) <- partitionM doesDirectoryExist [dir </> x | x <- xs, not $ isBadDir x]
14+
rest <- concatMapM getDirectoryContentsRecursive dirs
15+
return $ files++rest
16+
where
17+
isBadDir x = "." `isPrefixOf` x || "_" `isPrefixOf` x
18+
19+
20+
partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a], [a])
21+
partitionM f [] = return ([], [])
22+
partitionM f (x:xs) = do
23+
res <- f x
24+
(as,bs) <- partitionM f xs
25+
return ([x|res]++as, [x|not res]++bs)
26+
27+
28+
concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
29+
concatMapM f = liftM concat . mapM f
30+
31+
32+
headDef :: a -> [a] -> a
33+
headDef x [] = x
34+
headDef x (y:ys) = y
35+
36+
37+
limit :: Int -> String -> String
38+
limit n s = if null post then s else pre ++ "..."
39+
where (pre,post) = splitAt n s
40+
41+

0 commit comments

Comments
 (0)