Created
May 8, 2014 10:54
-
-
Save techtangents/f7b3ed8f374a7418ea56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
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