pub struct Reducer { /* private fields */ }
Expand description
An uncomplicated builder of arguments for InputStream’s reduce method.
RowStream::reduce()
accepts a vector of Aggregates. Use this struct to
define them using the methods listed below.
Example
use csvsc::prelude::*;
use encoding::all::UTF_8;
InputStreamBuilder::from_paths(&["test/assets/1.csv"]).unwrap().build().unwrap()
.reduce(vec![
Reducer::with_name("rows").count(),
])
.into_iter();
Implementations
sourceimpl Reducer
impl Reducer
sourcepub fn with_name(name: &str) -> Reducer
pub fn with_name(name: &str) -> Reducer
Start building an aggregate by giving it a column name. This column will be present in the output row and will contain the aggregated value.
sourcepub fn count(self) -> Box<dyn Aggregate>
pub fn count(self) -> Box<dyn Aggregate>
Creates an aggregate that counts all Ok rows of the stream.
Example
Reducer::with_name("rows").count();
sourcepub fn custom<'a, A>(reducer: A) -> Box<dyn Aggregate + 'a> where
A: Aggregate + 'a,
pub fn custom<'a, A>(reducer: A) -> Box<dyn Aggregate + 'a> where
A: Aggregate + 'a,
A custom reducer, this method is not strictrly needed because you can
just pass Box::new(YourReducer::new())
to .reduce()
but it exists
for consistency.
Example
use csvsc::prelude::*;
#[derive(Debug)]
struct MyAggregate {
colname: String,
state: (f64, f64),
}
impl Aggregate for MyAggregate {
fn update(&mut self, headers: &Headers, row: &Row) -> csvsc::error::Result<()> {
let x: f64 = headers.get_field(row, "x").unwrap().parse().unwrap();
let y: f64 = headers.get_field(row, "y").unwrap().parse().unwrap();
self.state.0 += x;
self.state.1 += y;
Ok(())
}
fn colname(&self) -> &str {
&self.colname
}
fn value(&self) -> String {
format!("{},{}", self.state.0, self.state.1)
}
}
Reducer::custom(MyAggregate {
colname: String::from("custom"),
state: (0.0, 0.0),
});
sourcepub fn of_column(self, name: &str) -> Reducer
pub fn of_column(self, name: &str) -> Reducer
Most aggregates need a source column, use this string as source column name.
Example
Reducer::with_name("richest").of_column("income").max(0.0);
sourcepub fn average(self) -> Option<Box<dyn Aggregate>>
pub fn average(self) -> Option<Box<dyn Aggregate>>
Compute the average of the values in the specified column, if a value cannot be parsed to f64 returns an error row.
Example
Reducer::with_name("average").of_column("income").average();
sourcepub fn last(self, init: &str) -> Option<Box<dyn Aggregate>>
pub fn last(self, init: &str) -> Option<Box<dyn Aggregate>>
A simple reducer that takes the last value it sees or uses the argument given as default.
Useful for just keeping a column used for grouping in an aggregate.
Example
Reducer::with_name("name").of_column("name").last("-");
sourcepub fn max<'a, T>(self, init: T) -> Option<Box<dyn Aggregate + 'a>> where
T: Display + Debug + PartialOrd + FromStr + Copy + 'a,
<T as FromStr>::Err: Debug,
pub fn max<'a, T>(self, init: T) -> Option<Box<dyn Aggregate + 'a>> where
T: Display + Debug + PartialOrd + FromStr + Copy + 'a,
<T as FromStr>::Err: Debug,
Return the maximum of the specified column’s values and use init
as
default if the value cannot be parsed to T.
init
is both used to specify the type to parse to and the default.
A sensible value for it would be a number smaller than all the values
you expect from your data
Example
use std::f64;
Reducer::with_name("max").of_column("height").max(f64::NEG_INFINITY);
sourcepub fn min<'a, T>(self, init: T) -> Option<Box<dyn Aggregate + 'a>> where
T: Display + Debug + PartialOrd + FromStr + Copy + 'a,
<T as FromStr>::Err: Debug,
pub fn min<'a, T>(self, init: T) -> Option<Box<dyn Aggregate + 'a>> where
T: Display + Debug + PartialOrd + FromStr + Copy + 'a,
<T as FromStr>::Err: Debug,
Return the minimum of the specified column’s values and use init
as
default if the value cannot be parsed to T.
init
is both used to specify the type to parse to and the default.
A sensible value for it would be a number higher than all the values
you expect from your data.
Example
use std::f64;
Reducer::with_name("min").of_column("height").min(f64::INFINITY);
sourcepub fn sum<'a, T>(self, init: T) -> Option<Box<dyn Aggregate + 'a>> where
T: Display + Debug + AddAssign + FromStr + Copy + 'a,
<T as FromStr>::Err: Debug,
pub fn sum<'a, T>(self, init: T) -> Option<Box<dyn Aggregate + 'a>> where
T: Display + Debug + AddAssign + FromStr + Copy + 'a,
<T as FromStr>::Err: Debug,
Return the sum of the specified column’s values and use init
as
default if the value cannot be parsed to T.
init
is both used to specify the type to parse to and the default.
A sensible value for it would be 0.0
Example
Reducer::with_name("sum").of_column("height").sum(0.0);
sourcepub fn product<'a, T>(self, init: T) -> Option<Box<dyn Aggregate + 'a>> where
T: Display + Debug + MulAssign + FromStr + Copy + 'a,
<T as FromStr>::Err: Debug,
pub fn product<'a, T>(self, init: T) -> Option<Box<dyn Aggregate + 'a>> where
T: Display + Debug + MulAssign + FromStr + Copy + 'a,
<T as FromStr>::Err: Debug,
Return the product of the specified column’s values and use init
as
default if the value cannot be parsed to T.
init
is both used to specify the type to parse to and the default.
A sensible value for it would be 1.0
Example
Reducer::with_name("product").of_column("height").product(1.0);
sourcepub fn with_closure<'a, F, C>(
self,
f: F,
init: C
) -> Option<Box<dyn Aggregate + 'a>> where
F: FnMut(C, &str) -> Result<C> + 'a,
C: Display + 'a,
pub fn with_closure<'a, F, C>(
self,
f: F,
init: C
) -> Option<Box<dyn Aggregate + 'a>> where
F: FnMut(C, &str) -> Result<C> + 'a,
C: Display + 'a,
Use a closure to compute an aggregate for the specified column.
Here you have absolute freedom.
The first argument of the closure is the accumulator, the second is the
current row’s value. init
will be used as starter.
Example
Reducer::with_name("closure").of_column("col").with_closure(|acc, cur| {
Ok(acc + cur.parse::<i32>().unwrap())
}, 0).unwrap();
Auto Trait Implementations
impl RefUnwindSafe for Reducer
impl Send for Reducer
impl Sync for Reducer
impl Unpin for Reducer
impl UnwindSafe for Reducer
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more