ndmitchell / extra

Extra Haskell functions
Other
93 stars 37 forks source link

proposal to add filesWithExtension #82

Closed juhp closed 3 years ago

juhp commented 3 years ago

I would like to suggest adding a couple of small functions to System.Directory.Extra:

filesWithExtension :: FilePath -- directory
                   -> String   -- file extension
                   -> IO [FilePath]

fileWithExtension :: FilePath -- directory
                  -> String   -- file extension
                  -> IO (Maybe FilePath)

How does that sound? If it is okay I am happy to open a PR for it.

ndmitchell commented 3 years ago

The second one seems redundant - it's easier to do listToMaybe over the first, or whatever is the right policy to reduce many files down to one.

For filesWithExtension, what's the benefit over just doing the listFiles and a filter? My concern is you often want to list files directly, with recursion, and with recursion avoiding . prefixed directories or similar. By the time you have three variants it starts to explode badly in terms of complexity.

juhp commented 3 years ago

This an implementation of filesWithExtension:

filesWithExtension :: FilePath -> String -> IO [FilePath]
filesWithExtension dir ext =
  filter (ext `isExtensionOf`) <$> listFiles dir

I admit it looks pretty straightforward (specially when one remembers to use isExtensionOf). Maybe listFilesWithExtension would be a better name.

I see your point, though I find myself using filesWithExtension (and less often fileWithExtension) in various projects: it is a bit less typing. :-)

ndmitchell commented 3 years ago

Yeah, I think I'll close this for now, and not add it. But if in 6 months time or so you've needed to use that one a lot, but not any of the other variations with isExtensionOf, it might be worth considering.