Skip to content

Instantly share code, notes, and snippets.

@katzkb
Created February 18, 2019 07:11
Show Gist options
  • Save katzkb/0bdf794642ee4da80aa5f099bb15f6e6 to your computer and use it in GitHub Desktop.
Save katzkb/0bdf794642ee4da80aa5f099bb15f6e6 to your computer and use it in GitHub Desktop.
import scala.math.abs
// 気温リスト1~12月
val temperatureSeq:Seq[Int] =
Seq(4, 5, 11, 17, 19, 22, 28, 28, 22, 19, 14, 7)
def diffTemperature1(tSeq: Seq[Int]) = {
@scala.annotation.tailrec
def f(diffSeq:Seq[Int], tSeq: Seq[Int]): Seq[Int] = {
tSeq match {
case head +: second +: tail =>
f(diffSeq :+ abs(head - second), second +: tail)
case _ =>
diffSeq
}
}
f(Seq.empty, temperatureSeq)
}
diffTemperature1(temperatureSeq)
// -> res0: Seq[Int] = List(1, 6, 6, 2, 3, 6, 0, 6, 3, 5, 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment