Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if a file is empty. #212

Open
GregorySchwartz opened this issue Feb 10, 2017 · 4 comments
Open

Check if a file is empty. #212

GregorySchwartz opened this issue Feb 10, 2017 · 4 comments

Comments

@GregorySchwartz
Copy link

isEmpty :: FilePath -> Bool

Replacing "[ -s diff.txt ]" in bash.

@Gabriella439
Copy link
Owner

So you can do this using a combination of fold, null, and input, like this:

import Prelude hiding (FilePath)
import Turtle

import qualified Control.Foldl.Text

isEmpty :: MonadIO io => FilePath -> io Bool
isEmpty path = fold (input path) Control.Foldl.Text.null

The main reason I suggest combining these three functions is to reduce API proliferation. Otherwise I'd need to create similar utilities for other Folds in the same module for consistency

@PierreR
Copy link
Contributor

PierreR commented Feb 24, 2017

Very useful trick, thanks.
I guess to make the compiler happy it should be

isFileEmpty :: MonadIO io => FilePath -> io Bool
isFileEmpty path =
  fold (lineToText <$> input path) Control.Foldl.Text.null

@Gabriella439
Copy link
Owner

Actually, there may be a simpler solution, which is to use Control.Foldl.null instead of Control.Foldl.Text.null (in other words, to check if the file has 0 lines instead of checking that it has 0 characters)

@PierreR
Copy link
Contributor

PierreR commented Feb 24, 2017

Control.Foldl.null does seem to work nicely for me. Nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants