Skip to content

Instantly share code, notes, and snippets.

@benkolera
Created January 20, 2015 20:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benkolera/6a3622aa5c6ceb99dcf8 to your computer and use it in GitHub Desktop.
Save benkolera/6a3622aa5c6ceb99dcf8 to your computer and use it in GitHub Desktop.
hush :: Either e a -> Maybe a
hush = either (const Nothing) Just
toId :: JObject -> Maybe Number
toId o = fromNumber <|> fromString
where
fromNumber = hush $ o .? "id"
fromString = do
s <- hush $ o .? "id"
mfilter (isNan >>> not) Just (readFloat s)
toUsername :: JObject -> Maybe String
toUsername o = hush $ o .? "username"
toEmail :: Json -> Maybe String
toEmail o = hush $ o .? "email"
toIsActive :: Json -> Maybe Boolean
toIsActive o = either (const (Just true)) Just $ o .? "isActive"
toUser :: Json -> Maybe User
toUser json = do
obj <- toObject
id <- toId obj
username <- toUsername obj
isActive <- toIsActive obj
return { id: id
, username: username
, email: toEmail json
, isActive: isActive
}
@davidchambers
Copy link

Thanks! hush improves readability significantly. I've incorporated your improvements into my answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment