Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Control.Lens
- -- from Control.Error
- hush :: Either e a -> Maybe a
- hush (Left _) = Nothing
- hush (Right a) = Just a
- -------------------------------------------------------------------------------
- data Lossy e a b = Lossy {
- previewE :: a -> Either e b
- , reviewE :: b -> a
- }
- lossy :: (b -> a) -> (a -> Either e b)-> Lossy e a b
- lossy = flip Lossy
- hushLossy :: Lossy e a b -> Prism' a b
- hushLossy l = prism' (reviewE l) (hush . (previewE l))
- -------------------------------------------------------------------------------
- newtype MyInt = MyInt Int deriving (Show)
- data MyIntError = TooBig
- | TooSmall
- deriving (Show)
- myIntLossy :: Lossy MyIntError Int MyInt
- myIntLossy = lossy toI fromI
- where
- toI (MyInt i) = i
- fromI i
- | i < 0 = Left TooSmall
- | i > 10 = Left TooBig
- | otherwise = Right (MyInt i)
- useLikePrism :: Either MyIntError MyInt
- useLikePrism = previewE myIntLossy 42
- useAsPrism :: Maybe MyInt
- useAsPrism = 42 ^? hushLossy myIntLossy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement