Created
June 21, 2010 23:12
-
-
Save maoe/447680 to your computer and use it in GitHub Desktop.
How to use view patterns
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
{-# LANGUAGE PatternGuards #-} | |
{-# LANGUAGE ViewPatterns #-} | |
module ViewPatterns where | |
import Data.List | |
data EMail = EMail User Domain deriving Show | |
type User = String | |
type Domain = String | |
view :: String -> Maybe EMail | |
view ss | |
| (user@(_:_), _:domain) | |
<- break (== '@') ss = Just (EMail user domain) | |
view _ = Nothing | |
email :: String -> Maybe EMail | |
email (view -> x) = x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment