use crate::math::{complex::Complex, context::Context}; 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 fn is_empty(&self) -> bool { self.args.len() == 0 } pub fn data(&self) -> &[Complex] { &self.args } } pub trait Function { fn eval(&self, args: FunctionArgument, ctx: &Context) -> Result; } #[macro_export] macro_rules! functions { {$($x:expr => $y:expr), *} => { { let mut h : HashMap> = HashMap::new(); $( h.insert($x.to_string(), Arc::new($y)); )* h } }; } // Some default implementations impl Function for T where T: Fn(Complex) -> Complex, { fn eval(&self, args: FunctionArgument, _ctx: &Context) -> Result { if args.len() == 1 { Ok(self(args.get(0))) } else { Err("too many arguments".to_string()) } } } pub struct EmptyFunction {} impl Function for EmptyFunction { fn eval(&self, _args: FunctionArgument, _ctx: &Context) -> Result { Err("function not implemented in this scope".to_string()) } }