I just want a function that returns true
if all the elements of a List[Integer]
follow each other, i.e.
noGaps(List(3,4,5)) // true
noGaps(List(4,3,5)) // false
noGaps(List(3,4,6)) // false
I have something that works but it's a bit verbose - what's the most elegant solution?
How about this?
def noGaps(xs: Seq[Int]) =
xs.size < 2 || xs.sliding(2).forall { case Seq(x, y) => y == x + 1 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With