Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created February 10, 2015 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/7c736e48433706d12f6a to your computer and use it in GitHub Desktop.
Save shigemk2/7c736e48433706d12f6a to your computer and use it in GitHub Desktop.
def isSorted[A](as: Array[A], gt: (A,A) => Boolean): Boolean = {
@annotation.tailrec
def loop(n:Int) : Boolean =
if (n >= as.length - 1) true
else if (gt(as(n), as(n+1))) loop(n+1)
else false
loop(0)
}
println(isSorted[Int](Array(1,2,3), _<_))
println(isSorted[Int](Array(3,1,2), _<_))
println(isSorted[Int](Array(), _<_))
println(isSorted[Int](Array(1), _<_))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment