Skip to content

Instantly share code, notes, and snippets.

@colindean
Created October 13, 2015 20:57
Show Gist options
  • Save colindean/9b883b93379f7c1a0e40 to your computer and use it in GitHub Desktop.
Save colindean/9b883b93379f7c1a0e40 to your computer and use it in GitHub Desktop.
QuantityCheck - a wrapper around Seq#lengthCompare because I like matching words instead of remembering -1/0/1
class QuantityCheck[T](thing: Seq[T])
case class JustRight[T](thing: Seq[T]) extends QuantityCheck(thing)
case class TooFew[T](thing: Seq[T]) extends QuantityCheck(thing)
case class TooMany[T](thing: Seq[T]) extends QuantityCheck(thing)
object QuantityCheck {
def apply[T](number: Int)(seqLike: Seq[T]): QuantityCheck[T] = {
seqLike.lengthCompare(number) match {
case x if x < 0 => TooFew(seqLike)
case x if x == 0 => JustRight(seqLike)
case x if x > 0 => TooMany(seqLike)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment