diff --git a/CHANGELOG.md b/CHANGELOG.md index e865e20..6d14d09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Add Pipeline `select` method - Add Pipeline `filter` & `filter_col` methods - Add Pipeline `from_pipelines` constructor for merging pipelines together +- Add `count` transformer - Remember row order in transform_into - Include source index in errors diff --git a/src/transform.rs b/src/transform.rs index a261bd3..5700a0f 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -52,7 +52,7 @@ impl Transformer { value: "".to_string(), }) } - /// Sum the values in this column + /// Sum the values in this column. pub fn sum<'a, N>(self, init: N) -> Box where N: Display + AddAssign + FromStr + Clone + 'a, @@ -63,7 +63,7 @@ impl Transformer { value: init, }) } - /// Reduce the values from this column into a single value using a closure + /// Reduce the values from this column into a single value using a closure. pub fn reduce<'a, R, V>(self, reduce: R, init: V) -> Box where R: FnMut(V, &str) -> Result + 'a, @@ -76,6 +76,14 @@ impl Transformer { value: init, }) } + + /// Count the rows that were reduced into this row. + pub fn count(self) -> Box { + Box::new(Count { + name: self.name, + value: 0, + }) + } } struct KeepUnique { @@ -182,3 +190,21 @@ where self.name.clone() } } + +struct Count { + name: String, + value: u128, +} +impl Transform for Count { + fn add_row(&mut self, _headers: &Headers, _row: &Row) -> Result<(), Error> { + self.value += 1; + Ok(()) + } + + fn value(&self) -> String { + self.value.to_string() + } + fn name(&self) -> String { + self.name.clone() + } +}