algorithms icon indicating copy to clipboard operation
algorithms copied to clipboard

add top (?) that takes max

Open dblock opened this issue 14 years ago • 2 comments

Not sure if this is a good name for it. We do a lot of fetching an array from the top of the heap (priority queue).

def top(max)
  results = []
  while results.size < max and size > 0 do 
    results << pop 
  end
  results
end

If you think it's a good idea, I'll make a proper pull request with a UT.

dblock avatar Feb 12 '11 15:02 dblock

@dblock A good name for this might be take because it looks like this works same way as Array#take.

GarrisonJ avatar Jan 18 '24 01:01 GarrisonJ

@GarrisonJ this is such an obvious and excellent answer, thank you

dblock avatar Jan 18 '24 17:01 dblock