Created
April 23, 2026 11:36
-
-
Save jmcph4/5f38193ad1f6218963ca8811b37f58aa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| module Book where | |
| import Control.Applicative (liftA2) | |
| maximumMaybe :: Ord a => [a] -> Maybe a | |
| maximumMaybe [] = Nothing | |
| maximumMaybe xs = Just (maximum xs) | |
| minimumMaybe :: Ord a => [a] -> Maybe a | |
| minimumMaybe [] = Nothing | |
| minimumMaybe xs = Just (minimum xs) | |
| type Price = Float | |
| type Quantity = Integer | |
| type PriceDelta = Float | |
| data Book = Book [(Price, Quantity)] [(Price, Quantity)] deriving (Eq, Show) | |
| bids :: Book -> [(Price, Quantity)] | |
| bids (Book xs ys) = xs | |
| asks :: Book -> [(Price, Quantity)] | |
| asks (Book xs ys) = ys | |
| bestBid :: Book -> Maybe Price | |
| bestBid book = maximumMaybe $ map fst $ bids $ book | |
| bestAsk :: Book -> Maybe Price | |
| bestAsk book = maximumMaybe $ map fst $ asks $ book | |
| bba :: Book -> (Maybe Price, Maybe Price) | |
| bba book = (bestBid book, bestAsk book) | |
| spread :: Book -> Maybe PriceDelta | |
| spread book = liftA2 (-) (bestAsk book) (bestBid book) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment