Skip to content

Instantly share code, notes, and snippets.

@jchia
Created May 25, 2018 06:37
Show Gist options
  • Select an option

  • Save jchia/d785c6dfaea8dfd6745a3315b2ce70d1 to your computer and use it in GitHub Desktop.

Select an option

Save jchia/d785c6dfaea8dfd6745a3315b2ce70d1 to your computer and use it in GitHub Desktop.
Guards
data MappingRow = MappingRow { date :: Date, prod :: Sym, tenor1v :: Sym, tenor2v :: Sym } deriving Show
convertRow :: [MySQLValue] -> IO MappingRow
convertRow sqlRow = eitherStrToMonadError $
-- I have to define some otherwise-useless x just so that I can use guards. Is there a better way?
-- Why is the syntax for guards so restrictive?
let x
| [MySQLInt32U date, MySQLText prod, MySQLInt16U tenor1v, MySQLInt16U tenor2v] <- sqlRow = do
prod' <- maybeToEither "Bad symbol" $ symFromStrMay prod
let (tenor1v', tenor2v') =
(tenor1v, tenor2v) & each %~ (prod' <>) . symFromStrEx . sformat (left 4 '0')
pure $ MappingRow (yyyymmdd . fromIntegral $ date) prod' tenor1v' tenor2v'
| otherwise = fail "Bad column count or column types"
in x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment