on master help sign in
Add documentation to String and replace stringy examples in List docs
14b60a0Evan Czaplicki
libraries/List.elm+6−5
@@ -69,7 +69,7 @@ isEmpty xs =
69map : (a -> b) -> [a] -> [b]
69map : (a -> b) -> [a] -> [b]
70map = Native.List.map
70map = Native.List.map
71
71
72−{-| Reduce a list from the left: `(foldl (::) [] "gateman" == "nametag")` -}
72+{-| Reduce a list from the left: `(foldl (::) [] [1,2,3] == [3,2,1])` -}
73foldl : (a -> b -> b) -> b -> [a] -> b
73foldl : (a -> b -> b) -> b -> [a] -> b
74foldl = Native.List.foldl
74foldl = Native.List.foldl
75
75
@@ -100,12 +100,12 @@ scanl1 : (a -> a -> a) -> [a] -> [a]
100scanl1 = Native.List.scanl1
100scanl1 = Native.List.scanl1
101
101
102{-| Keep only elements that satisfy the predicate:
102{-| Keep only elements that satisfy the predicate:
103−`(filter isLower "AaBbCc" == "abc")`
103+`(filter isEven [1..6] == [2,4,6])`
104-}
104-}
105filter : (a -> Bool) -> [a] -> [a]
105filter : (a -> Bool) -> [a] -> [a]
106filter = Native.List.filter
106filter = Native.List.filter
107
107
108−{-| Determine the length of a list: `(length "innumerable" == 11)` -}
108+{-| Determine the length of a list: `(length [1,2,3] == 3)` -}
109length : [a] -> Int
109length : [a] -> Int
110length = Native.List.length
110length = Native.List.length
111
111
@@ -131,7 +131,8 @@ or = foldl (||) False
131
131
132{-| Concatenate a list of appendable things:
132{-| Concatenate a list of appendable things:
133
133
134+ concat [[1,2],[3],[4,5]] == [1,2,3,4,5]
134− concat ["tree","house"] == "treehouse"
135+ concat ["tree","house"] == "treehouse"
135-}
136-}
136concat : [appendable] -> appendable
137concat : [appendable] -> appendable
137concat = Native.List.concat
138concat = Native.List.concat
@@ -201,7 +202,7 @@ join = Native.List.join
201
202
202{-| Places the given value between all members of the given list.
203{-| Places the given value between all members of the given list.
203
204
204− intersperse ' ' "INCEPTION" == "I N C E P T I O N"
205+ intersperse "on" ["turtles","turtles","turtles"] == ["turtles","on","turtles","on","turtles"]
205-}
206-}
206intersperse : a -> [a] -> [a]
207intersperse : a -> [a] -> [a]
207intersperse sep xs =
208intersperse sep xs =
libraries/String.elm+113−47
@@ -12,7 +12,7 @@ module String where
12@docs sub, left, right, dropLeft, dropRight
12@docs sub, left, right, dropLeft, dropRight
13
13
14# Check for Substrings
14# Check for Substrings
15−@docs contains, startsWith, endsWith, indices, indexes
15+@docs contains, startsWith, endsWith, indexes, indices
16
16
17# Conversion To Numbers
17# Conversion To Numbers
18@docs toInt, toFloat
18@docs toInt, toFloat
@@ -28,38 +28,42 @@ Cosmetic operations such as padding with extra characters or trimming whitespace
28import Native.String
28import Native.String
29import Maybe (Maybe)
29import Maybe (Maybe)
30
30
31−{-|
31+{-| Check if a string is empty `(isEmpty "" == True)` -}
32−-}
33isEmpty : String
32isEmpty : String
34isEmpty = Native.String.isEmpty
33isEmpty = Native.String.isEmpty
35
34
36−{-|
35+{-| Add a character to the beginning of a string -}
37−-}
38cons : Char -> String -> String
36cons : Char -> String -> String
39cons = Native.String.cons
37cons = Native.String.cons
40
38
41−{-|
39+{-| Split a non-empty string into its head and tail. This lets you
40+pattern match on strings exactly as you would with lists.
41+
42+ uncons "abc" == Just ('a',"bc")
43+ uncons "" == Nothing
42-}
44-}
43uncons : String -> Maybe (Char, String)
45uncons : String -> Maybe (Char, String)
44uncons = Native.String.uncons
46uncons = Native.String.uncons
45
47
46−{-|
48+{-| Get the length of a string `(length "innumerable" == 11)` -}
47−-}
48length : String -> Int
49length : String -> Int
49length = Native.String.length
50length = Native.String.length
50
51
51−{-|
52+{-| Transform every character in a string
53+
54+ map (\c -> if c == '/' then '.' else c) "a/b/c" == "a.b.c"
52-}
55-}
53map : (Char -> Char) -> String -> String
56map : (Char -> Char) -> String -> String
54map = Native.String.map
57map = Native.String.map
55
58
56−{-|
59+{-| Keep only the characters that satisfy the predicate:
60+
61+ filter isDigit "R2-D2" == "22"
57-}
62-}
58filter : (Char -> Bool) -> String -> String
63filter : (Char -> Bool) -> String -> String
59filter = Native.String.filter
64filter = Native.String.filter
60
65
61−{-|
66+{-| Reverse a string. `(reverse "stressed" == "desserts")` -}
62−-}
63reverse : String -> String
67reverse : String -> String
64reverse = Native.String.reverse
68reverse = Native.String.reverse
65
69
@@ -73,135 +77,197 @@ foldl = Native.String.foldl
73foldr : (Char -> b -> b) -> b -> String -> b
77foldr : (Char -> b -> b) -> b -> String -> b
74foldr = Native.String.foldr
78foldr = Native.String.foldr
75
79
76−{-|
80+{-| Split a string using a given separator.
81+
82+ split "," "cat,dog,cow" == ["cat","dog","cow"]
83+ split "/" "home/evan/Desktop/" == ["home","evan","Desktop"]
77-}
84-}
78split : String -> String -> [String]
85split : String -> String -> [String]
79split = Native.String.split
86split = Native.String.split
80
87
81−{-|
88+{-| Put many strings together with a given separator.
89+
90+ join " " ["cat","dog","cow"] == "cat dog cow"
91+ join "/" ["home","evan","Desktop"] == "home/evan/Desktop"
82-}
92-}
83join : String -> [String] -> String
93join : String -> [String] -> String
84join = Native.String.join
94join = Native.String.join
85
95
86−{-|
96+{-| Repeat a string N times `(repeat 3 "ha" == "hahaha")` -}
87−-}
88repeat : Int -> String -> String
97repeat : Int -> String -> String
89repeat = Native.String.repeat
98repeat = Native.String.repeat
90
99
100+{-| Take a substring given a start and end index. Negative indexes
101+are taken starting from the *end* of the list.
102+
103+ sub 7 9 "snakes on a plane!" == "on"
104+ sub 0 6 "snakes on a plane!" == "snakes"
105+ sub 0 -7 "snakes on a plane!" == "snakes on a"
106+ sub -6 -1 "snakes on a plane!" == "plane"
107+-}
91sub : Int -> Int -> String -> String
108sub : Int -> Int -> String -> String
92sub = Native.String.sub
109sub = Native.String.sub
93
110
94−{-|
111+{-| Take N characters from the left side of a string. -}
95−-}
96left : Int -> String -> String
112left : Int -> String -> String
97left = Native.String.left
113left = Native.String.left
98
114
99−{-|
115+{-| Take N characters from the right side of a string. -}
100−-}
101right : Int -> String -> String
116right : Int -> String -> String
102right = Native.String.right
117right = Native.String.right
103
118
104−{-|
119+{-| Drop N characters from the left side of a string. -}
105−-}
106dropLeft : Int -> String -> String
120dropLeft : Int -> String -> String
107dropLeft = Native.String.dropLeft
121dropLeft = Native.String.dropLeft
108
122
109−{-|
123+{-| Drop N characters from the right side of a string. -}
110−-}
111dropRight : Int -> String -> String
124dropRight : Int -> String -> String
112dropRight = Native.String.dropRight
125dropRight = Native.String.dropRight
113
126
114−{-|
127+{-| Pad a string on both sides until it has a given length.
128+
129+ pad 5 ' ' "1" == " 1 "
130+ pad 5 ' ' "11" == " 11 "
131+ pad 5 ' ' "121" == " 121 "
115-}
132-}
116pad : Int -> Char -> String -> String
133pad : Int -> Char -> String -> String
117pad = Native.String.pad
134pad = Native.String.pad
118
135
119−{-|
136+{-| Pad a string on the left until it has a given length.
137+
138+ pad 5 '.' "1" == "....1"
139+ pad 5 '.' "11" == "...11"
140+ pad 5 '.' "121" == "..121"
120-}
141-}
121padLeft : Int -> Char -> String -> String
142padLeft : Int -> Char -> String -> String
122padLeft = Native.String.padLeft
143padLeft = Native.String.padLeft
123
144
124−{-|
145+{-| Pad a string on the right until it has a given length.
146+
147+ pad 5 '.' "1" == "1...."
148+ pad 5 '.' "11" == "11..."
149+ pad 5 '.' "121" == "121.."
125-}
150-}
126padRight : Int -> Char -> String -> String
151padRight : Int -> Char -> String -> String
127padRight = Native.String.padRight
152padRight = Native.String.padRight
128
153
129−{-|
154+{-| Get rid of whitespace on both sides of a string.
155+
156+ trim " hats \n" == "hats"
130-}
157-}
131trim : String -> String
158trim : String -> String
132trim = Native.String.trim
159trim = Native.String.trim
133
160
134−{-|
161+{-| Get rid of whitespace on the left of a string.
162+
163+ trimLeft " hats \n" == "hats \n"
135-}
164-}
136trimLeft : String -> String
165trimLeft : String -> String
137trimLeft = Native.String.trimLeft
166trimLeft = Native.String.trimLeft
138
167
139−{-|
168+{-| Get rid of whitespace on the left of a string.
169+
170+ trimRight " hats \n" == " hats"
140-}
171-}
141trimRight : String -> String
172trimRight : String -> String
142trimRight = Native.String.trimRight
173trimRight = Native.String.trimRight
143
174
144−{-|
175+{-| Break a string into words, splitting on chunks of whitespace.
176+
177+ words "How are \t you? \n Good?" == ["How","are","you?","Good?"]
145-}
178-}
146words : String -> [String]
179words : String -> [String]
147words = Native.String.words
180words = Native.String.words
148
181
149−{-|
182+{-| Break a string into lines, splitting on newlines.
183+
184+ lines "How are you?\nGood? == ["How are you?", "Good?"]
150-}
185-}
151lines : String -> [String]
186lines : String -> [String]
152lines = Native.String.lines
187lines = Native.String.lines
153
188
154−{-|
189+{-| Convert a string to all upper case. Useful for case insensitive comparisons
190+and VIRTUAL YELLING.
155-}
191-}
156toUpper : String -> String
192toUpper : String -> String
157toUpper = Native.String.toUpper
193toUpper = Native.String.toUpper
158
194
159−{-|
195+{-| Convert a string to all lower case. Useful for case insensitive comparisons. -}
160−-}
161toLower : String -> String
196toLower : String -> String
162toLower = Native.String.toLower
197toLower = Native.String.toLower
163
198
164−{-|
199+{-| Check to see if *any* characters satisfy a predicate.
200+
201+ any isDigit "90210" == True
202+ any isDigit "R2-D2" == True
203+ any isDigit "heart" == False
165-}
204-}
166any : (Char -> Bool) -> String -> Bool
205any : (Char -> Bool) -> String -> Bool
167any = Native.String.any
206any = Native.String.any
168
207
169−{-|
208+{-| Check to see if *all* characters satisfy a predicate.
209+
210+ all isDigit "90210" == True
211+ all isDigit "R2-D2" == False
212+ any isDigit "heart" == False
170-}
213-}
171all : (Char -> Bool) -> String -> Bool
214all : (Char -> Bool) -> String -> Bool
172all = Native.String.all
215all = Native.String.all
173
216
174−{-|
217+{-| See if a string contains another one.
218+
219+ contains "the" "theory" == True
220+ contains "hat" "theory" == False
221+ contains "THE" "theory" == False
175-}
222-}
176contains : String -> String -> Bool
223contains : String -> String -> Bool
177contains = Native.String.contains
224contains = Native.String.contains
178
225
179−{-|
226+{-| See if a string starts with another one.
227+
228+ startsWith "the" "theory" == True
229+ startsWith "ory" "theory" == False
180-}
230-}
181startsWith : String -> String -> Bool
231startsWith : String -> String -> Bool
182startsWith = Native.String.startsWith
232startsWith = Native.String.startsWith
183
233
184−{-|
234+{-| See if a string ends with another one.
235+
236+ endsWith "the" "theory" == False
237+ endsWith "ory" "theory" == True
185-}
238-}
186endsWith : String -> String -> Bool
239endsWith : String -> String -> Bool
187endsWith = Native.String.endsWith
240endsWith = Native.String.endsWith
188
241
189−{-|
242+{-| Get all of the indexes for a substring.
190−-}
191−indices : String -> String -> [Int]
192−indices = Native.String.indexes
193
243
194−{-|
244+ indexes "i" "Mississippi" == [1,4,7,10]
245+ indexes "ss" "Mississippi" == [2,5]
246+ indexes "needle" "haystack" == []
195-}
247-}
196indexes : String -> String -> [Int]
248indexes : String -> String -> [Int]
197indexes = Native.String.indexes
249indexes = Native.String.indexes
198
250
199−{-|
251+{-| Alias for `indexes` -}
252+indices : String -> String -> [Int]
253+indices = Native.String.indexes
254+
255+{-| Try to convert a string into an int, failing on improperly formatted strings.
256+
257+ toInt "123" == Just 123
258+ toInt "-42" == Just -42
259+ toInt "3.1" == Nothing
260+ toInt "31a" == Nothing
200-}
261-}
201toInt : String -> Maybe Int
262toInt : String -> Maybe Int
202toInt = Native.String.toInt
263toInt = Native.String.toInt
203
264
204−{-|
265+{-| Try to convert a string into a float, failing on improperly formatted strings.
266+
267+ toFloat "123" == Just 123.0
268+ toFloat "-42" == Just -42.0
269+ toFloat "3.1" == Just 3.1
270+ toFloat "31a" == Nothing
205-}
271-}
206toFloat : String -> Maybe Float
272toFloat : String -> Maybe Float
207toFloat = Native.String.toFloat
273toFloat = Native.String.toFloat