Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Last active December 31, 2015 03:28
Show Gist options
  • Save Heimdell/7927306 to your computer and use it in GitHub Desktop.
Save Heimdell/7927306 to your computer and use it in GitHub Desktop.
Utility to remove spaces from haskell sources. Recursive.
import Control.Monad
import Control.Arrow
import System.Directory (getDirectoryContents, doesDirectoryExist)
import System.Environment (getArgs)
import System.FilePath.Posix ((</>))
import Data.String.Utils (startswith, endswith, rstrip)
main :: IO ()
main = do
args <- getArgs
let exts = if null args then stdExt else args
clearDir exts "."
stdExt :: [String]
stdExt = map ('.' :) ["hs", "c++", "h++", "c", "cpp", "h", "rb"]
clearDir :: [String] -> FilePath -> IO ()
clearDir exts dir = clearDir' dir
where
clearDir' dir = do
nodes <- getDirectoryContents dir
forM_ nodes $ \node' -> do
let node = dir </> node'
is_a_dir <- doesDirectoryExist node
let isTargetFile file = any' exts (file `endswith'`)
if is_a_dir
then
unless (node' `startswith'` ".") $ do
putStrLn $ "Entering %" % node
clearDir' node
else
when (isTargetFile node) $ do
putStrLn $ "Clearing %" % node
clearFile node
clearFile :: FilePath -> IO ()
clearFile file = do
content <- readFile file
materialize content
let clear = lines >>> map rstrip >>> unlines
writeFile file $ clear content
materialize :: [a] -> IO ()
materialize list = length list `seq` return ()
any' = flip any
endswith' = flip endswith
startswith' = flip startswith
(%) :: String -> String -> String
format % item
= let (before, '%' : after) = break (== '%') format
in join [before, item, after]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment