Last active
August 30, 2017 12:40
-
-
Save juanbono/98631b60edbcbc3fd4a7998cafa8326e to your computer and use it in GitHub Desktop.
Haskell script that checks the number of files in a specified directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env stack | |
| {- stack script | |
| --resolver lts-9.1 | |
| --package turtle | |
| --package foldl | |
| --package system-filepath | |
| --package bytestring | |
| --package text | |
| --package containers | |
| -} | |
| {-# LANGUAGE OverloadedStrings #-} | |
| import Turtle | |
| import Data.List | |
| import Prelude hiding (FilePath) | |
| import qualified Control.Foldl as Fold | |
| import qualified Filesystem.Path.CurrentOS as FS | |
| import qualified Data.ByteString.Lazy as BS | |
| import qualified Data.Set as Set | |
| import Control.Applicative | |
| import qualified Data.ByteString.Char8 as C8 | |
| -- this is faster than the Text-based version | |
| dropPackageVersion :: FilePath -> C8.ByteString | |
| dropPackageVersion = fst . C8.breakEnd (== '-') . FS.encode | |
| parseDirectory :: Parser FilePath | |
| parseDirectory = argPath "DIR" "The packages directory" | |
| countFiles :: MonadIO io => FilePath -> io Int | |
| countFiles dir = fold (ls dir) Fold.length | |
| nubWith :: Ord b => (a -> b) -> [a] -> Set.Set b | |
| nubWith = func Set.empty | |
| where | |
| func s _ [] = s | |
| func set f (x:xs) | |
| | Set.member x' set = func set f xs | |
| | otherwise = func (Set.insert x' set) f xs | |
| where x' = f x | |
| listOfFiles :: MonadIO io => FilePath -> io [FilePath] | |
| listOfFiles dir = fold (ls dir) Fold.list | |
| numberOfDifferentPackages :: MonadIO io => FilePath -> io Int | |
| numberOfDifferentPackages directory = do | |
| listOfPackages <- listOfFiles directory | |
| return $ (length . nubWith dropPackageVersion) listOfPackages | |
| main :: IO () | |
| main = do | |
| directory <- options "" parseDirectory | |
| total <- countFiles directory | |
| diffs <- numberOfDifferentPackages directory | |
| printf ("# packages in this directory: " %w% "\n") total | |
| printf ("# different packages in this directory: " %w% "\n") diffs | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Docs about Haskell scripts