Skip to content

Instantly share code, notes, and snippets.

@tybl
Last active August 29, 2015 14:04
Show Gist options
  • Save tybl/e2121311cf5d940c2cc2 to your computer and use it in GitHub Desktop.
Save tybl/e2121311cf5d940c2cc2 to your computer and use it in GitHub Desktop.
Rust struct for calculating the running average(mean)
use std::num::{ zero, one };
struct Mean {
mean: f64,
count: u64,
}
impl Mean {
fn consider(&mut self, other: f64) {
self.count = self.count + one();
let delta = other - self.mean;
self.mean = self.mean + (delta / self.count as f64);
}
fn mean(&self) -> f64 {
self.mean
}
fn new() -> Mean {
Mean { mean: zero(), count: zero() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment