Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created October 21, 2010 19:30
Show Gist options
  • Save debasishg/639146 to your computer and use it in GitHub Desktop.
Save debasishg/639146 to your computer and use it in GitHub Desktop.
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