Skip to content

Instantly share code, notes, and snippets.

@techtangents
Created May 8, 2014 10:54
Show Gist options
  • Save techtangents/f7b3ed8f374a7418ea56 to your computer and use it in GitHub Desktop.
Save techtangents/f7b3ed8f374a7418ea56 to your computer and use it in GitHub Desktop.
import scala.collection.immutable.Queue
import scalaz.Monoid
case class BQ[A](private val q: Queue[A], n: Int) {
private def bound(qq: Queue[A]): Queue[A] = {
val s = qq.size
if (s > n)
qq.drop(n - s)
else
qq
}
def enqueue(a: A): BQ[A] =
new BQ[A](bound(q.enqueue(a)), n)
def append(x: BQ[A]): BQ[A] =
new BQ[A](bound(q ++ x.q), n)
}
object BQ {
def empty[A](n: Int) = new BQ(Queue.empty[A], n)
implicit def bqMonoid[A] = new Monoid[A] {
override def zero: A = ???
override def append(f1: A, f2: => A): A = ???
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment