diff --git a/src/distribution/empirical.rs b/src/distribution/empirical.rs index b2ab498d..21832b3c 100644 --- a/src/distribution/empirical.rs +++ b/src/distribution/empirical.rs @@ -31,7 +31,7 @@ impl Ord for NonNan { /// /// let samples = vec![0.0, 5.0, 10.0]; /// -/// let empirical = Empirical::from_vec(samples); +/// let empirical = Empirical::from_iter(samples); /// assert_eq!(empirical.mean().unwrap(), 5.0); /// ``` #[derive(Clone, PartialEq, Debug)] @@ -65,14 +65,6 @@ impl Empirical { }) } - pub fn from_vec(src: Vec) -> Empirical { - let mut empirical = Empirical::new().unwrap(); - for elt in src.into_iter() { - empirical.add(elt); - } - empirical - } - pub fn add(&mut self, data_point: f64) { if !data_point.is_nan() { self.sum += 1.; @@ -174,6 +166,16 @@ impl std::fmt::Display for Empirical { } } +impl FromIterator for Empirical { + fn from_iter>(iter: T) -> Self { + let mut empirical = Self::new().unwrap(); + for elt in iter { + empirical.add(elt); + } + empirical + } +} + #[cfg(feature = "rand")] #[cfg_attr(docsrs, doc(cfg(feature = "rand")))] impl ::rand::distributions::Distribution for Empirical { @@ -243,7 +245,7 @@ mod tests { #[test] fn test_cdf() { let samples = vec![5.0, 10.0]; - let mut empirical = Empirical::from_vec(samples); + let mut empirical = Empirical::from_iter(samples); assert_eq!(empirical.cdf(0.0), 0.0); assert_eq!(empirical.cdf(5.0), 0.5); assert_eq!(empirical.cdf(5.5), 0.5); @@ -271,7 +273,7 @@ mod tests { #[test] fn test_sf() { let samples = vec![5.0, 10.0]; - let mut empirical = Empirical::from_vec(samples); + let mut empirical = Empirical::from_iter(samples); assert_eq!(empirical.sf(0.0), 1.0); assert_eq!(empirical.sf(5.0), 0.5); assert_eq!(empirical.sf(5.5), 0.5);