Created
October 21, 2010 19:30
-
-
Save debasishg/639146 to your computer and use it in GitHub Desktop.
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
makeTrade :: [(String, Maybe String)] -> Maybe Trade | |
-- using applicative | |
makeTrade alist = | |
Trade <$> lookup1 "account" alist | |
<*> lookup1 "instrument" alist | |
<*> (read <$> (lookup1 "market" alist)) | |
<*> lookup1 "ref_no" alist | |
<*> (read <$> (lookup1 "unit_price" alist)) | |
<*> (read <$> (lookup1 "quantity" alist)) | |
-- using monadic lift and ap | |
makeTrade alist = | |
Trade `liftM` lookup1 "account" alist | |
`ap` lookup1 "instrument" alist | |
`ap` (read `liftM` (lookup1 "market" alist)) | |
`ap` lookup1 "ref_no" alist | |
`ap` (read `liftM` (lookup1 "unit_price" alist)) | |
`ap` (read `liftM` (lookup1 "quantity" alist)) | |
lookup1 key alist = case lookup key alist of | |
Just (Just s@(_:_)) -> Just s | |
_ -> Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment