use crate::complex::Complex; pub struct FunctionArgument { args : Vec, } impl FunctionArgument { pub fn new(args: Vec) -> Self { Self { args } } pub fn get(&self, i : usize) -> Complex { self.args[i] } pub fn len(&self) -> usize { self.args.len() } } pub trait Function { fn eval(&self, args: FunctionArgument) -> Result; } #[macro_export] macro_rules! functions { ({$($x:expr => $y:expr), *}) => { { let mut h : HashMap> = HashMap::new(); $( h.insert($x.to_string(), Box::new($y)); )* h } }; } // Some default implementations impl Function for T where T: Fn(Complex) -> Complex, { fn eval(&self, args: FunctionArgument) -> Result { if args.len() == 1 { Ok(self(args.get(0))) } else { Err("too many arguments".to_string()) } } }