Skip to content

Instantly share code, notes, and snippets.

@devth
Last active September 15, 2015 15:24
Show Gist options
  • Save devth/ddea1a4cc768a186ff43 to your computer and use it in GitHub Desktop.
Save devth/ddea1a4cc768a186ff43 to your computer and use it in GitHub Desktop.
// Compute center-weighted moving average of equally spaced values, some of which may be None
// period should be odd
def centerWeightedAverage (values: Iterable[Option[Double]], period: Int): Seq[Option[Double]] = {
// prepend and append period/2 Nones
val valuesWithBuffers = List.fill(period / 2)(None) ++ values ++ List.fill(period / 2)(None)
valuesWithBuffers
.sliding(period)
.map(_.flatten)
.map(x => if (x.size > 0) Some(x.sum.toDouble / x.size) else None)
.take(values.size)
.toList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment