Module
Data.Either.Nested
- Package
- purescript-either
- Repository
- purescript/purescript-either
Utilities for n-eithers: sums types with more than two terms built from nested eithers.
Nested eithers arise naturally in sum combinators. You shouldn't represent sum data using nested eithers, but if combinators you're working with create them, utilities in this module will allow to to more easily work with them, including translating to and from more traditional sum types.
data Color = Red Number | Green Number | Blue Number
fromEither3 :: Either3 Number Number Number -> Color
fromEither3 = either3 Red Green Blue
toEither3 :: Color -> Either3 Number Number Number
toEither3 (Red v) = in1 v
toEither3 (Green v) = in2 v
toEither3 (Blue v) = in3 v
#(\/) Source
Operator alias for Data.Either.either (right-associative / precedence 6)
The \/
operator alias for the either
function allows easy matching on nested Eithers. For example, consider the function
f :: (Int \/ String \/ Boolean) -> String
f (Left x) = show x
f (Right (Left y)) = y
f (Right (Right z)) = if z then "Yes" else "No"
The \/
operator alias allows us to rewrite this function as
f :: (Int \/ String \/ Boolean) -> String
f = show \/ identity \/ if _ then "Yes" else "No"