Created
March 28, 2015 14:46
-
-
Save Centaur/95a5f7ea1f8efdb9df5b to your computer and use it in GitHub Desktop.
isSorted
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@annotation.tailrec | |
def isSorted[A](as: Seq[A], ordered: (A, A)=>Boolean):Boolean = { | |
as match { | |
case Seq() => true | |
case Seq(_) => true | |
case Seq(head, second, rest@_*) => | |
ordered(head, second) && isSorted(second +: rest, ordered) | |
} | |
} | |
val f = (_:Int) < (_:Int) | |
assert(isSorted(Array(1, 2 , 6, 10), f)) | |
assert(!isSorted(Array(4, 2 , 6, 10), f)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment