Skip to content

Instantly share code, notes, and snippets.

@Centaur
Created March 28, 2015 14:46
Show Gist options
  • Save Centaur/95a5f7ea1f8efdb9df5b to your computer and use it in GitHub Desktop.
Save Centaur/95a5f7ea1f8efdb9df5b to your computer and use it in GitHub Desktop.
isSorted
@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