Giovanni Azua - 1 year ago 47

Scala Question

I have an array

`val x : Array[Double]`

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

`i`

`foldLeft`

`foldRight`

Answer Source

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