Skip to content

Instantly share code, notes, and snippets.

@jmcph4
Created April 23, 2026 11:36
Show Gist options
  • Select an option

  • Save jmcph4/5f38193ad1f6218963ca8811b37f58aa to your computer and use it in GitHub Desktop.

Select an option

Save jmcph4/5f38193ad1f6218963ca8811b37f58aa to your computer and use it in GitHub Desktop.
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