git.iko.soy/typeable/cabal2nix
on master help sign in
listDirectoryFilesBySuffix: simplify the implementation

Match suffixes first, then do the I/O to see whether matching paths refers to a file or not. Ought to be more efficient. There's no point doing the I/O if the suffix doesn't match.

commit7a72711d56b6deaff828c566512ea0b88146e582 authorPeter Simons <simons@cryp.to> dateFri 21 Dec 2018, 20:17 (8 years ago) parentaf7cf68
1 file changed+3−11
test/Main.hs+3−11
test/Main.hs+3−11
@@ -99,14 +99,6 @@ testExecutable exe cabalFile = do
9999-- * Helper functions
100100-------------------------------------------------------------------------------
101101
102−-- | Find all *files* in the given directory. This function does not recurse,
103−-- nor does it include the special entries @'.'@ or @'..'@ in the result.
104−
105−listDirectoryFiles :: FilePath -> IO [FilePath]
106−listDirectoryFiles dirPath = do
107− entries <- listDirectory dirPath
108− filterM doesFileExist (map (dirPath </>) entries)
109−
110102-- | Find all *files* in the given directory that end with the given suffix.
111103-- This function does not recurse, nor does it include the special entries
112104-- @'.'@ or @'..'@ in the result.
@@ -119,9 +111,9 @@ listDirectoryFiles dirPath = do
119111-- we prefer our own simpler code.
120112
121113listDirectoryFilesBySuffix :: String -> FilePath -> IO [FilePath]
122−listDirectoryFilesBySuffix suff =
123− fmap (filter ((suff ==) . takeExtension)) . listDirectoryFiles
124−
114+listDirectoryFilesBySuffix suff dirPath = do
115+ entries <- listDirectory dirPath
116+ filterM doesFileExist [ dirPath </> e | e <- entries, takeExtension e == suff ]
125117
126118-- | A variant of 'writeFile' that appends a newline to the end of the buffer
127119-- before writing.