1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
--- ---------------------------------------------------------------------------- --- Library with some useful operations for the `Either` data type. --- --- @author Bjoern Peemoeller --- @version March 2015 --- @category general --- ---------------------------------------------------------------------------- {-# OPTIONS_CYMAKE -Wno-incomplete-patterns #-} module Either ( Either (..) , either , lefts , rights , isLeft , isRight , fromLeft , fromRight , partitionEithers ) where --- Extracts from a list of `Either` all the `Left` elements in order. lefts :: [Either a b] -> [a] lefts x = [a | Left a <- x] --- Extracts from a list of `Either` all the `Right` elements in order. rights :: [Either a b] -> [b] rights x = [a | Right a <- x] --- Return `True` if the given value is a `Left`-value, `False` otherwise. isLeft :: Either a b -> Bool isLeft (Left _) = True isLeft (Right _) = False --- Return `True` if the given value is a `Right`-value, `False` otherwise. isRight :: Either a b -> Bool isRight (Left _) = False isRight (Right _) = True --- Extract the value from a `Left` constructor. fromLeft :: Either a _ -> a fromLeft (Left x) = x --- Extract the value from a `Right` constructor. fromRight :: Either _ b -> b fromRight (Right x) = x --- Partitions a list of `Either` into two lists. --- All the `Left` elements are extracted, in order, to the first --- component of the output. Similarly the `Right` elements are extracted --- to the second component of the output. partitionEithers :: [Either a b] -> ([a],[b]) partitionEithers = foldr (either left right) ([],[]) where left a (l, r) = (a:l, r) right a (l, r) = (l, a:r) |