Created
February 23, 2016 10:42
-
-
Save robkuz/2f052d6a0a992bfa16ad to your computer and use it in GitHub Desktop.
How to extract a nested unbalanced structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- I was thinking on how to normalize a structure like | |
-- (Either String (Either String Int)) into | |
-- Either String Int | |
-- my first attempt was to pattern match | |
normalizer :: (Either String (Either String Int)) -> Either String Int | |
normalizer n = do | |
case n of | |
Left l -> Left l | |
Right r -> r | |
-- but that somehow liked unhaskellish | |
-- so I tried this | |
normalizer' :: (Either String (Either String Int)) -> Either String Int | |
normalizer' n = either Left id n | |
-- much better! | |
-- the empiphany is this | |
-- a) Data constructors a functions too - Left :: a -> Left a | |
-- b) if one wants to pipe a value to a function an get the same input back. id is your friend because | |
-- id :: a -> a | |
r = normalizer $ Right (Right 10) | |
l = normalizer $ Left "Oh my" | |
r' = normalizer' $ Right (Right 10) | |
l' = normalizer' $ Left "Oh my" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also use
join
fromControl.Monad
.