Skip to content

Instantly share code, notes, and snippets.

@trylks
Created August 6, 2013 13:10
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 trylks/6164315 to your computer and use it in GitHub Desktop.
Save trylks/6164315 to your computer and use it in GitHub Desktop.
I'm still learning Scala so this may be very wrong for a number of reasons. I'm not even sure about the meaning of the <: operator
// for: http://stackoverflow.com/questions/14011181/how-can-i-find-the-index-of-the-maximum-value-in-a-list-in-scala
@tailrec
final def maxindex[T <: Ordered[T]](list:Iterable[T], defaultIndex:Int = -1, defaultValue:T=None, currentIndex:Int = 0):(Int, T) = list match {
case Nil => (defaultIndex, defaultValue)
case head::tail =>
if (head > defaultValue)
maxindex(tail, currentIndex, head, currentIndex+1)
else
maxindex(tail, defaultIndex, defaultValue, currentIndex+1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment