Question: I write a python telnet client to communicate with a server through telnet. However, many people tell me that it's no secure. How can I convert it to ssh? Should I need to totally rewrite my program?

Answer: 
```
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)
}
// res1: Option[Int] = Some(2)

```

`sliding` returns an iterator, so that should only traverse the list once.

If you are interested to find which two elements have the smallest gap, 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))
// res4: (Int, Int) = (37,39)

```
```
val sorted = List(1, 5, 15, 37, 39, 42, 50)
(sorted.tail,sorted).zipped.map(_-_).min
//res2: Int = 2

```

**[Edit]**

You can use a fold as well:

```
sorted.tail.foldLeft((sorted.head,Int.MaxValue))((x,y) => (y, math.min(y-x._1,x._2)))._2

```