It'll be useful to be able to fetch the top N items. I ended up doing the following, but it seems to be useful enough to be provided in the API. For example, Python heapq has nlargest and nsmallest.
minN :: Int -> PQ -> ([(String, Int, Tree)], PQ)
minN 0 q = ([], q)
minN n q = case Q.minView q of
Nothing -> ([], q)
Just (k, p, v, q') ->
let (xs, q'') = minN (n - 1) q'
in ((k, p, v) : xs, q'')
It'll be useful to be able to fetch the top N items. I ended up doing the following, but it seems to be useful enough to be provided in the API. For example, Python
heapq
hasnlargest
andnsmallest
.