dererXII.scalashogi_2.13.5.0.2.source-code.DecayingStats.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalashogi_2.13 Show documentation
Show all versions of scalashogi_2.13 Show documentation
Shogi API written in scala. Immutable and free of side effects.
package shogi
trait DecayingRecorder {
def record(value: Float): DecayingStats
}
final case class DecayingStats(
mean: Float,
deviation: Float,
decay: Float
) extends DecayingRecorder {
def record(value: Float): DecayingStats = {
val delta = mean - value
copy(
mean = value + decay * delta,
deviation = decay * deviation + (1 - decay) * math.abs(delta)
)
}
def record[T](values: Iterable[T])(implicit n: Numeric[T]): DecayingStats =
values.foldLeft(this) { (s, v) =>
s record n.toFloat(v)
}
}
final case class EmptyDecayingStats(
deviation: Float,
decay: Float
) extends DecayingRecorder {
def record(value: Float) =
DecayingStats(
mean = value,
deviation = deviation,
decay = decay
)
}
object DecayingStats {
val empty = EmptyDecayingStats
}