-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseFile.hs
More file actions
70 lines (50 loc) · 1.75 KB
/
Copy pathParseFile.hs
File metadata and controls
70 lines (50 loc) · 1.75 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module ParseFile where
import System.IO
import Data.Char
import Data.List.Split
--type Name = String
data MessageD = MessageD
{ from :: String
, to :: String
, message :: String
} deriving (Show)
commaParse :: String -> [Int]
commaParse s = let xs' = splitOneOf "," s in
map read xs'
fromFile :: FilePath -> IO [MessageD]
fromFile fp = do
fileLines <- readFileLines fp
--putStrLn $ show $ length fileLines
mds <- parseLines fileLines
return mds
parseLine :: String -> MessageD
parseLine s = let from = takeWhile (not . comma) s
rest = drop ((length from) + 1) s
rest' = dropWhile (isSpace) rest
to = takeWhile (not . colon) rest'
rest'' = drop ((length to) + 1) rest'
message = dropWhile (isSpace) rest'' in
MessageD from to message
where comma :: Char -> Bool
comma x = x == ','
colon :: Char -> Bool
colon x = x == ':'
parseLines :: [String] -> IO [MessageD]
parseLines xs = do
let res = map parseLine xs
--putStrLn $ show $ length res
return res
where f :: String -> MessageD
f s = let from = takeWhile (not . comma) s
rest = drop ((length from) + 1) s
rest' = dropWhile (isSpace) rest
to = takeWhile (not . colon) rest'
rest'' = drop ((length to) + 1) rest'
message = dropWhile (isSpace) rest'' in
MessageD from to message
comma :: Char -> Bool
comma x = x == ','
colon :: Char -> Bool
colon x = x == ':'
readFileLines :: FilePath -> IO [String]
readFileLines = fmap lines . readFile