Giovanni Azua - 9 months ago 32

Scala Question

I have an array

`val x : Array[Double]`

`x(i) <= x(i+1)`

`i`

`foldLeft`

`foldRight`

Answer

Consider this:

```
def isMonotonic(arr:Array[Int]) =
if (arr.isEmpty) true
else (arr, arr.tail).zipped.forall {case (a,b) => a <= b}
```

Simplified solution (thanks to @som-snytt):

```
def isMonotonic(arr:Array[Int]) =
(arr, arr.drop(1)).zipped.forall (_ <= _)
```

Source (Stackoverflow)