Reputation:
So i'm reading the "Functional Programing in Scala" and I'm doing exercise 2.2
Implement isSorted, which checks whether an Array[A] is sorted according to a given comparison function: def isSorted[A] (as: Array[A], ordered: (A,A) => Boolean) : Boolean = {
My solution, which works
{
def orderByValue(a : Int, b : Int): Boolean ={
println (a + " " + b )
if( a<= b) true
else false
}
//exercise, def to check wheter an Array[a] is sorted
def isSorted[A] (as: Array[A], ordered: (A,A) => Boolean) : Boolean = {
var result = true;
def loop(n: Int) {
if (result == false)
false //Return false on first instance
else if (n < as.length -1) {
result = ordered(as(n), as(n + 1))
loop(n + 1)
}
else
result
}
loop(0)
result
}
}
Was pretty proud of my first go, but thought it doesn't look very functional, so I asked a friend and he came back with
{
def isSorted2[A] (as: Array[A], ordered: (A,A) => Boolean) : Boolean = {
as.sliding(2,1).foldLeft(true)((res, a ) => ordered(a(0), a(1)) && res)
}
}
Like wtf, how this black magic possible. I understand everything from as.sliding(2,1).foldLeft(true)((res, a ) => ordered(a(0), a(1)), but the && respart, I can't see to find any documentation on it. I know it folds the result of the previous operation, but how does it work, what's this operation called.
Upvotes: 2
Views: 292
Reputation: 11587
def isSorted2[A] (as: Array[A], ordered: (A,A) => Boolean) : Boolean = {
as.sliding(2,1).foldLeft(true)((res, a ) => ordered(a(0), a(1)) && res)
}
&&
is a normal logical operator here. The signature of ordered function is (A,A) => Boolean
which means it takes two parameters of type A and true a Boolean indicating if argument 1 is greater or smaller than argument2. res is accumulator for the foldLeft function which is set to false even if one of the item is not in proper sort order. This is ensured by the second curried parameter
(res, a ) => ordered(a(0), a(1)) && res
so the simple logic here is that, if the current adjacent items in the list is sorted and all the previous items in the list is also sorted then the list is sorted till the current position. so if we move this current position to the end of the list and if the list is still sorted then the entire list is sorted. But even if one item is not in sorted order compared to its adjacent neighbour than the acc is set to false and the output of the overall method will be false.
The API definition of the foldLeft method is available here
Upvotes: 3