Created
August 6, 2013 13:10
-
-
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
This file contains 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
// 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