Skip to content

Instantly share code, notes, and snippets.

package pl.japila.scalania.s99
object S99_P03 {
def nth[T](k: Int, ts: List[T]): Option[T] = {
if (k > 0) ts match {
case a :: tail => nth(k - 1, tail)
case Nil => None
}
else if (k == 0) Some(ts.head)
else None
package pl.japila.scalania.s99
object S99_P02 {
def penultimate[T](ts: Seq[T]): T = ts match {
case a :: b :: Nil => Some(a)
case a:: tail => None
case _ => penultimate(ts.tail)
}
}