Bounded priority queue in ZIO

Viewed 107

There is TPriorityQueue in ZIO which is unbounded. Is there a way to adapt it to become bounded?

1 Answers

If you don't care about performance too much you can do something quick and dirty like this:

case class TPW(tpq: TPriorityQueue[Int]) {
  def offer(e: Int) = ZSTM.whenM(tpq.size.map(_ < 50))(tpq.offer(e))
  // .. other methods
}

This would be a dropping queue.

Related