Skip to content

Instantly share code, notes, and snippets.

@ryoppy
Created December 12, 2013 09:20
Show Gist options
  • Save ryoppy/7925267 to your computer and use it in GitHub Desktop.
Save ryoppy/7925267 to your computer and use it in GitHub Desktop.
練習用にmin実装
// not tailrec... :(
def min(l: Seq[Int]): Int = l match {
case h :: t if t.isEmpty => h
case h :: t => min(t) match {
case r if h < r => h
case r => r
}
}
// tailrec! :)
def min(l: Seq[Int]): Int = {
@tailrec
def min(l: Seq[Int], n: Int): Int = l match {
case h :: t => min(t, if (n < h) n else h)
case _ => n
}
min(l, l.head)
}
def min(l: Seq[Int]): Int = l.reduceLeft { (n, i) => if (n < i) n else i }
l.min
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment