jaspervdj / psqueues

Priority Search Queues in three different flavors for Haskell
https://hackage.haskell.org/package/psqueues
Other
64 stars 24 forks source link

minN/maxN #56

Open asarkar opened 7 months ago

asarkar commented 7 months ago

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'')