Giovanni Azua - 8 months ago 27

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 (_ <= _)
```