Skip to content

Instantly share code, notes, and snippets.

@maoe
Created June 21, 2010 23:12
Show Gist options
  • Save maoe/447680 to your computer and use it in GitHub Desktop.
Save maoe/447680 to your computer and use it in GitHub Desktop.
How to use view patterns
{-# 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