Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. import Control.Lens
  2.  
  3.  
  4. -- from Control.Error
  5. hush :: Either e a -> Maybe a
  6. hush (Left _) = Nothing
  7. hush (Right a) = Just a
  8.  
  9. -------------------------------------------------------------------------------
  10. data Lossy e a b = Lossy {
  11. previewE :: a -> Either e b
  12. , reviewE :: b -> a
  13. }
  14.  
  15.  
  16. lossy :: (b -> a) -> (a -> Either e b)-> Lossy e a b
  17. lossy = flip Lossy
  18.  
  19.  
  20. hushLossy :: Lossy e a b -> Prism' a b
  21. hushLossy l = prism' (reviewE l) (hush . (previewE l))
  22.  
  23.  
  24. -------------------------------------------------------------------------------
  25. newtype MyInt = MyInt Int deriving (Show)
  26.  
  27.  
  28. data MyIntError = TooBig
  29. | TooSmall
  30. deriving (Show)
  31.  
  32.  
  33. myIntLossy :: Lossy MyIntError Int MyInt
  34. myIntLossy = lossy toI fromI
  35. where
  36. toI (MyInt i) = i
  37. fromI i
  38. | i < 0 = Left TooSmall
  39. | i > 10 = Left TooBig
  40. | otherwise = Right (MyInt i)
  41.  
  42.  
  43. useLikePrism :: Either MyIntError MyInt
  44. useLikePrism = previewE myIntLossy 42
  45.  
  46.  
  47. useAsPrism :: Maybe MyInt
  48. useAsPrism = 42 ^? hushLossy myIntLossy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement