-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.hs
More file actions
102 lines (87 loc) · 2.98 KB
/
Copy pathUtilities.hs
File metadata and controls
102 lines (87 loc) · 2.98 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module Utilities
( beheaded_by
, join
, join_container
, join_sep
, is_empty_string
, string_to_bool
, string_to_int
, begins_with
, splitted_with
, at
, readMaybe
) where
beheaded_by :: String -> String -> Maybe String
string `beheaded_by` target = case (string, target) of
(s, "") -> Just s
("", x:xs) -> Nothing
(s:ss, x:xs) -> if s == x
then ss `beheaded_by` xs
else Nothing
join :: [String] -> String
join = foldr (++) ""
join_sep :: String -> [String] -> String
join_sep div [] = []
join_sep div (x:[]) = x
join_sep div (x:y:xs) = x ++ div ++ (join_sep div $ y:xs)
join_container :: String -> String -> ([String] -> String)
join_container begin end xs = begin ++ (join xs) ++ end
join_sep_container :: String -> String -> String -> ([String] -> String)
join_sep_container begin end div xs = begin ++ (join_sep div xs) ++ end
is_empty_string :: String -> Bool
is_empty_string s = case s of
"" -> True
(x:xs) -> if x `elem` [' ', '\n']
then is_empty_string xs
else False
begins_with :: String -> String -> Bool
s `begins_with` x = case (s, x) of
(_ , "" ) -> True
("" , _ ) -> False
(s:ss, x:xs) -> if s == x
then ss `begins_with` xs
else False
-- if any string in the list begins that given string,
-- then Just that string
-- else, Nothing
filter_beginner :: [String] -> String -> Maybe (String, String)
filter_beginner [] _ = Nothing
filter_beginner (x:xs) s = case s `beheaded_by` x of
Nothing -> filter_beginner xs s
Just rest -> Just (x, rest)
-- breaks string into list of string,
-- where a new entry is started whenever a substring
-- matches one of the targets
splitted_with :: String -> [String] -> [String]
string `splitted_with` targets =
let helper :: String -> String -> [String]
helper "" work = case work of
"" -> []
_ -> [work]
helper ('\\':s:ss) work = helper ss (work ++ [s])
helper (s:ss) work = case filter_beginner targets (s:ss) of
-- no matches, so add char to work and continue
Nothing -> helper ss (work ++ [s])
-- matched `match`, so add work and match, and continue on rest
Just (match, rest) -> work : match : helper rest ""
in helper string ""
at :: [a] -> Int -> a
at list index =
let helper [] _ = error
$ "couldn't access element #" ++ (show index)
++ " in list of length " ++ (show $ length list)
helper (x:xs) 0 = x
helper (x:xs) i = xs `at` (i - 1)
in helper list index
readMaybe :: Read a => String -> Maybe a
readMaybe s = case reads s of
[(val, "")] -> Just val
_ -> Nothing
string_to_bool :: String -> Bool
string_to_bool s = case (readMaybe s :: Maybe Bool) of
Nothing -> error $ "could not parse '" ++ s ++ "' as Bool"
Just x -> x
string_to_int :: String -> Int
string_to_int s = case (readMaybe s :: Maybe Int) of
Nothing -> error $ "could not parse '" ++ s ++ "' as Int"
Just x -> x