Skip to content

Instantly share code, notes, and snippets.

@novakov-alexey-zz
Last active November 20, 2018 20:46
Show Gist options
  • Save novakov-alexey-zz/b8ff821ae9c808ff74fb535a19fe7969 to your computer and use it in GitHub Desktop.
Save novakov-alexey-zz/b8ff821ae9c808ff74fb535a19fe7969 to your computer and use it in GitHub Desktop.
observations.foldLeft(
List[Observation]() -> WindowStats(0, Int.MaxValue, Int.MinValue, 0)
) {
case ((series, stats), ob) =>
@tailrec
def calcStats(
st: WindowStats,
obs: List[Observation]): (WindowStats, List[Observation]) = {
obs match {
case h :: t if ob.time - obs.head.time > windowSize =>
val newStats = WindowStats(
st.sum - h.value,
if (t.nonEmpty) t.map(_.value).min else ob.value,
if (t.nonEmpty) t.map(_.value).max else ob.value,
t.length - 1
)
calcStats(newStats, t)
case _ =>
val window = obs :+ ob
val newStats = WindowStats(
st.sum + ob.value,
Math.min(st.min, ob.value),
Math.max(st.max, ob.value),
window.length
)
(newStats, window)
}
}
val (newStats, newSeries) = calcStats(stats, series)
f(ob, newStats) // f is a function which captures current statistics and prints it to a file
(newSeries, newStats)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment