-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseLines.hs
32 lines (26 loc) · 897 Bytes
/
ReverseLines.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Data.List
import Data.List.Split
import System.Environment
import System.IO
import System.Random
mapLines :: (String -> String) -> String -> String
mapLines someFn contents =
unlines . map someFn . lines $ contents
dispatch :: String -> String -> String
dispatch "reverse" contents =
mapLines reverse contents
dispatch "space" contents =
mapLines (intercalate " " . chunksOf 1) contents
dispatch "reverse-lines" contents =
(unlines . reverse . lines) contents
dispatch cmd _ =
error "No such command " ++ cmd
-- This is where the show begins!
main = do
args <- getArgs
let argsReversed = reverse args
fileIn = argsReversed !! 1
fileOut = argsReversed !! 0
commands = (reverse . drop 2) argsReversed
contents <- readFile fileIn
writeFile fileOut $ foldl (\contents' cmd -> dispatch cmd contents') contents commands