@@ -1,51 +1,76 @@
2module Either where
1module Either where
3+{-| Represents any data that can take two different types.
5+# Type and Constructors
6+@docs Either, Left, Right
9+@docs either, isLeft, isRight
12+@docs lefts, rights, partition, consLeft, consRight, consEither
4import List
16import List
6−-- Represents any data that can take two different types.
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.
11data Either a b = Left a | Right b
22data Either a b = Left a | Right b
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`.
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 }
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 }
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 }
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
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
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))`
36partition : [Either a b] -> ([a],[b])
49partition : [Either a b] -> ([a],[b])
37partition es = List.foldr consEither ([],[]) es
50partition es = List.foldr consEither ([],[]) es
52+{-| If `Left`, add the value to the front of the list.
53+ If `Right`, return the list unchanged
55+consLeft : Either a b -> [a] -> [a]
39consLeft e vs =
56consLeft e vs =
41 Left v -> v::vs
58 Left v -> v::vs
42 Right _ -> vs
59 Right _ -> vs
61+{-| If `Right`, add the value to the front of the list.
62+ If `Left`, return the list unchanged.
64+consRight : Either a b -> [b] -> [b]
44consRight e vs =
65consRight e vs =
46 Left _ -> vs
67 Left _ -> vs
47 Right v -> v::vs
68 Right v -> v::vs
70+{-| If `Left`, add the value to the left list.
71+ If `Right`, add the value to the right list.
73+consEither : Either a b -> ([a], [b]) -> ([a], [b])
49consEither e (ls,rs) =
74consEither e (ls,rs) =
51 Left l -> (l::ls,rs)
76 Left l -> (l::ls,rs)