on master help sign in
New style docs for Maybe and Either
ac635caMax New
libraries/Either.elm+39−14
@@ -1,51 +1,76 @@
1−
2module Either where
1module Either where
3
2
3+{-| Represents any data that can take two different types.
4+
5+# Type and Constructors
6+@docs Either, Left, Right
7+
8+# Taking Eithers apart
9+@docs either, isLeft, isRight
10+
11+# Eithers and Lists
12+@docs lefts, rights, partition, consLeft, consRight, consEither
13+
14+-}
15+
4import List
16import List
5
17
6−-- Represents any data that can take two different types.
7−--
8−-- This can also be used for error handling `(Either String a)` where error
18+{-| This can also be used for error handling `(Either String a)` where
9−-- messages are stored on the left, and the correct values (“right”
19+ error messages are stored on the left, and the correct values
10−-- values) are stored on the right.
20+ (“right” values) are stored on the right.
21+-}
11data Either a b = Left a | Right b
22data Either a b = Left a | Right b
12
23
13−-- Apply the first function to a `Left` and the second function to a `Right`.
24+{-| Apply the first function to a `Left` and the second function to a `Right`.
14−-- This allows the extraction of a value from an `Either`.
25+ This allows the extraction of a value from an `Either`.
26+-}
15either : (a -> c) -> (b -> c) -> Either a b -> c
27either : (a -> c) -> (b -> c) -> Either a b -> c
16either f g e = case e of { Left x -> f x ; Right y -> g y }
28either f g e = case e of { Left x -> f x ; Right y -> g y }
17
29
18−-- True if the value is a `Left`.
30+{-| True if the value is a `Left`. -}
19isLeft : Either a b -> Bool
31isLeft : Either a b -> Bool
20isLeft e = case e of { Left _ -> True ; _ -> False }
32isLeft e = case e of { Left _ -> True ; _ -> False }
21
33
22−-- True if the value is a `Right`.
34+{-| True if the value is a `Right`. -}
23isRight : Either a b -> Bool
35isRight : Either a b -> Bool
24isRight e = case e of { Right _ -> True ; _ -> False }
36isRight e = case e of { Right _ -> True ; _ -> False }
25
37
26−-- Keep only the values held in `Left` values.
38+{-| Keep only the values held in `Left` values. -}
27lefts : [Either a b] -> [a]
39lefts : [Either a b] -> [a]
28lefts es = List.foldr consLeft [] es
40lefts es = List.foldr consLeft [] es
29
41
30−-- Keep only the values held in `Right` values.
42+{-| Keep only the values held in `Right` values. -}
31rights : [Either a b] -> [b]
43rights : [Either a b] -> [b]
32rights es = List.foldr consRight [] es
44rights es = List.foldr consRight [] es
33
45
34−-- Split into two lists, lefts on the left and rights on the right. So we
46+{-| Split into two lists, lefts on the left and rights on the right. So we
35−-- have the equivalence: `(partition es == (lefts es, rights es))`
47+ have the equivalence: `(partition es == (lefts es, rights es))`
48+-}
36partition : [Either a b] -> ([a],[b])
49partition : [Either a b] -> ([a],[b])
37partition es = List.foldr consEither ([],[]) es
50partition es = List.foldr consEither ([],[]) es
38
51
52+{-| If `Left`, add the value to the front of the list.
53+ If `Right`, return the list unchanged
54+-}
55+consLeft : Either a b -> [a] -> [a]
39consLeft e vs =
56consLeft e vs =
40 case e of
57 case e of
41 Left v -> v::vs
58 Left v -> v::vs
42 Right _ -> vs
59 Right _ -> vs
43
60
61+{-| If `Right`, add the value to the front of the list.
62+ If `Left`, return the list unchanged.
63+-}
64+consRight : Either a b -> [b] -> [b]
44consRight e vs =
65consRight e vs =
45 case e of
66 case e of
46 Left _ -> vs
67 Left _ -> vs
47 Right v -> v::vs
68 Right v -> v::vs
48
69
70+{-| If `Left`, add the value to the left list.
71+ If `Right`, add the value to the right list.
72+-}
73+consEither : Either a b -> ([a], [b]) -> ([a], [b])
49consEither e (ls,rs) =
74consEither e (ls,rs) =
50 case e of
75 case e of
51 Left l -> (l::ls,rs)
76 Left l -> (l::ls,rs)
libraries/Maybe.elm+28−10
@@ -1,34 +1,52 @@
1−
2module Maybe where
1module Maybe where
3
2
3+{-| Represents an optional value.
4+
5+# Type and Constructors
6+@docs Maybe, Just, Nothing
7+
8+# Taking Maybes apart
9+@docs maybe, isJust, isNothing
10+
11+# Maybes and Lists
12+@docs cons, justs
13+-}
14+
4import Basics (not, (.))
15import Basics (not, (.))
5import List (foldr)
16import List (foldr)
6
17
7−-- The Maybe datatype. Useful when a computation may or may not
18+{-| The Maybe datatype. Useful when a computation may or may not
8−-- result in a value (e.g. logarithm is defined only for positive numbers).
19+ result in a value (e.g. logarithm is defined only for positive
20+ numbers).
21+-}
9data Maybe a = Just a | Nothing
22data Maybe a = Just a | Nothing
10
23
11−-- Apply a function to the contents of a `Maybe`.
24+{-| Apply a function to the contents of a `Maybe`.
12−-- Return default when given `Nothing`.
25+ Return default when given `Nothing`.
26+-}
13maybe : b -> (a -> b) -> Maybe a -> b
27maybe : b -> (a -> b) -> Maybe a -> b
14maybe b f m = case m of
28maybe b f m = case m of
15 Just v -> f v
29 Just v -> f v
16 Nothing -> b
30 Nothing -> b
17
31
18−-- Check if constructed with `Just`.
32+{-| Check if constructed with `Just`.
33+-}
19isJust : Maybe a -> Bool
34isJust : Maybe a -> Bool
20isJust = maybe False (\_ -> True)
35isJust = maybe False (\_ -> True)
21
36
22−-- Check if constructed with `Nothing`.
37+{-| Check if constructed with `Nothing`.
38+-}
23isNothing : Maybe a -> Bool
39isNothing : Maybe a -> Bool
24isNothing = not . isJust
40isNothing = not . isJust
25
41
26
42
27−-- If `Just`, adds the value to the front of the list.
43+{-| If `Just`, adds the value to the front of the list.
28−-- If `Nothing`, list is unchanged.
44+ If `Nothing`, list is unchanged.
45+-}
29cons : Maybe a -> [a] -> [a]
46cons : Maybe a -> [a] -> [a]
30cons mx xs = maybe xs (\x -> x :: xs) mx
47cons mx xs = maybe xs (\x -> x :: xs) mx
31
48
32−-- Filters out Nothings and extracts the remaining values.
49+{-| Filters out Nothings and extracts the remaining values.
50+-}
33justs : [Maybe a] -> [a]
51justs : [Maybe a] -> [a]
34justs = foldr cons []
52justs = foldr cons []