From 77cf9aa7535a1d9481f0bd3caeea26e2b85c5019 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 17 Jan 2024 22:30:02 +0100 Subject: migrate from f64 to own complex --- src/function.rs | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'src/function.rs') diff --git a/src/function.rs b/src/function.rs index 547ae0e..442d987 100644 --- a/src/function.rs +++ b/src/function.rs @@ -1,17 +1,51 @@ +use crate::complex::Complex; + pub struct FunctionArgument { - args : Vec, + args : Vec, } impl FunctionArgument { - pub fn new(args: Vec) -> Self { + pub fn new(args: Vec) -> Self { Self { args } } - pub fn get(&self, i : usize) -> f64 { + 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) -> f64; + 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()) + } + } } -- cgit v1.2.3-70-g09d2