Skip to content

Instantly share code, notes, and snippets.

@katzkb
Last active February 18, 2019 07:23
Show Gist options
  • Save katzkb/c52da3600a385ce226eea3838d610f9f to your computer and use it in GitHub Desktop.
Save katzkb/c52da3600a385ce226eea3838d610f9f 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 diffTemperature2(tSeq: Seq[Int]) = {
tSeq.sliding(2).foldLeft(Seq.empty[Int])((totalSeq, numSeq) =>
totalSeq :+ abs(numSeq.head - numSeq.last)
)
}
diffTemperature2(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