use crate::error::AdicResult;
use super::ComposedMapping;
pub trait Mapping<Input> {
type Output;
fn eval(&self, x: Input) -> AdicResult<Self::Output>;
fn compose<F, CInput>(self, inner: F) -> ComposedMapping<CInput, Self, F>
where F: Mapping<CInput, Output=Input>, Self: Sized {
ComposedMapping::new(self, inner)
}
}
pub trait IndexedMapping<Input>: Mapping<Input>{
fn eval_finite(&self, x: Input, num_terms: usize) -> AdicResult<Self::Output>;
}
impl<In, Out, F> Mapping<In> for F
where F: Fn(In) -> Out {
type Output = Out;
fn eval(&self, x: In) -> AdicResult<Out> {
Ok(self(x))
}
}
#[cfg(test)]
mod tests {
use super::Mapping;
#[test]
fn eval(){
let f =|x| x * x;
assert_eq!(f.eval(2), Ok(4));
let g = f.compose(f);
assert_eq!(g.eval(2), Ok(16));
}
}