@@ -26,28 +26,50 @@ data Either a b = Left a | Right b
27{-| Apply the first function to a `Left` and the second function to a `Right`.
27{-| Apply the first function to a `Left` and the second function to a `Right`.
28This allows the extraction of a value from an `Either`.
28This allows the extraction of a value from an `Either`.
30+ either (\n -> n + 1) sqrt (Left 4) == 5
31+ either (\n -> n + 1) sqrt (Right 4) == 2
33+ map : (a -> b) -> Either err a -> Either err b
34+ map f e = either Left (\x -> Right (f x)) e
30either : (a -> c) -> (b -> c) -> Either a b -> c
36either : (a -> c) -> (b -> c) -> Either a b -> c
31either f g e = case e of { Left x -> f x ; Right y -> g y }
37either f g e = case e of { Left x -> f x ; Right y -> g y }
33−{-| True if the value is a `Left`. -}
39+{-| True if the value is a `Left`.
41+ isLeft (Left "Cat") == True
42+ isLeft (Right 1123) == False
34isLeft : Either a b -> Bool
44isLeft : Either a b -> Bool
35isLeft e = case e of { Left _ -> True ; _ -> False }
45isLeft e = case e of { Left _ -> True ; _ -> False }
37−{-| True if the value is a `Right`. -}
47+{-| True if the value is a `Right`.
49+ isRight (Left "Cat") == False
50+ isRight (Right 1123) == True
38isRight : Either a b -> Bool
52isRight : Either a b -> Bool
39isRight e = case e of { Right _ -> True ; _ -> False }
53isRight e = case e of { Right _ -> True ; _ -> False }
41−{-| Keep only the values held in `Left` values. -}
55+{-| Keep only the values held in `Left` values.
57+ lefts [Left 3, Right 'a', Left 5, Right "eight"] == [3,5]
42lefts : [Either a b] -> [a]
59lefts : [Either a b] -> [a]
43lefts es = List.foldr consLeft [] es
60lefts es = List.foldr consLeft [] es
45−{-| Keep only the values held in `Right` values. -}
62+{-| Keep only the values held in `Right` values.
64+ rights [Left 3, Right 'a', Left 5, Right 'b'] == ['a','b']
46rights : [Either a b] -> [b]
66rights : [Either a b] -> [b]
47rights es = List.foldr consRight [] es
67rights es = List.foldr consRight [] es
49{-| Split into two lists, lefts on the left and rights on the right. So we
69{-| Split into two lists, lefts on the left and rights on the right. So we
50have the equivalence: `(partition es == (lefts es, rights es))`
70have the equivalence: `(partition es == (lefts es, rights es))`
72+ partition [Left 3, Right 'a', Left 5, Right 'b'] == ([3,5],['a','b'])
52partition : [Either a b] -> ([a],[b])
74partition : [Either a b] -> ([a],[b])
53partition es = List.foldr consEither ([],[]) es
75partition es = List.foldr consEither ([],[]) es