val sorted = List(1, 5, 15, 37, 39, 42, 50) sorted match { case Nil => None case List(a) => None case l => Some(l.sliding(2).map{case Seq(a, b) => math.abs(a - b)}.min) }
sliding returns an iterator, so it should go only once.
If you're interested in finding which two elements have the least space, you can also use minBy. So, here is another variation, just for fun.
sorted.view.zip(sorted.tail).minBy(t => math.abs(t._1 - t._2))
source share