on master help sign in
Add examples!
commit139f73a1e1cbba02f01801c1d8fbe0fc0e690af8 authorEvan Czaplicki <info@elm-lang.org> dateTue 18 Feb 2014, 18:33 (13 years ago) parent57c4b79
1 file changed+26−4
libraries/Either.elm+26−4
libraries/Either.elm+26−4
@@ -26,28 +26,50 @@ data Either a b = Left a | Right b
2626
2727{-| Apply the first function to a `Left` and the second function to a `Right`.
2828This allows the extraction of a value from an `Either`.
29+
30+ either (\n -> n + 1) sqrt (Left 4) == 5
31+ either (\n -> n + 1) sqrt (Right 4) == 2
32+
33+ map : (a -> b) -> Either err a -> Either err b
34+ map f e = either Left (\x -> Right (f x)) e
2935-}
3036either : (a -> c) -> (b -> c) -> Either a b -> c
3137either f g e = case e of { Left x -> f x ; Right y -> g y }
3238
33−{-| True if the value is a `Left`. -}
39+{-| True if the value is a `Left`.
40+
41+ isLeft (Left "Cat") == True
42+ isLeft (Right 1123) == False
43+-}
3444isLeft : Either a b -> Bool
3545isLeft e = case e of { Left _ -> True ; _ -> False }
3646
37−{-| True if the value is a `Right`. -}
47+{-| True if the value is a `Right`.
48+
49+ isRight (Left "Cat") == False
50+ isRight (Right 1123) == True
51+-}
3852isRight : Either a b -> Bool
3953isRight e = case e of { Right _ -> True ; _ -> False }
4054
41−{-| Keep only the values held in `Left` values. -}
55+{-| Keep only the values held in `Left` values.
56+
57+ lefts [Left 3, Right 'a', Left 5, Right "eight"] == [3,5]
58+-}
4259lefts : [Either a b] -> [a]
4360lefts es = List.foldr consLeft [] es
4461
45−{-| Keep only the values held in `Right` values. -}
62+{-| Keep only the values held in `Right` values.
63+
64+ rights [Left 3, Right 'a', Left 5, Right 'b'] == ['a','b']
65+-}
4666rights : [Either a b] -> [b]
4767rights es = List.foldr consRight [] es
4868
4969{-| Split into two lists, lefts on the left and rights on the right. So we
5070have the equivalence: `(partition es == (lefts es, rights es))`
71+
72+ partition [Left 3, Right 'a', Left 5, Right 'b'] == ([3,5],['a','b'])
5173-}
5274partition : [Either a b] -> ([a],[b])
5375partition es = List.foldr consEither ([],[]) es