Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. asNewtypeIf ::
  2.      (a -> b)    -- Type constructor
  3.   -> (a -> Bool) -- Constraint for newtype
  4.   -> a           -- Unwrapped value
  5.   -> Maybe b     -- Maybe wrapped value
  6. asNewtypeIf constructor predicate input
  7.   | predicate input = Just (constructor input)
  8.   | otherwise       = Nothing
  9.  
  10. --
  11.  
  12. newtype ShortString = ShortString String
  13.   deriving Show
  14.  
  15. makeShortString :: String -> Maybe ShortString
  16. makeShortString = ShortString `asNewtypeIf` isValidShortString
  17.   where
  18.     isValidShortString = (<= 3) . length
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement