rekka/meval-rs

Bind multiple variables

Limeth opened this issue · 2 comments

The library looks great so far!
Do you think it would be possible to add a function to bind several variables?

Something like the following:

pub fn bind_multiple<'a>(self, vars: &[&str])

It would be also helpful if we could evaluate the expression by providing a Map of variables tied with their values.

let expr = meval::Expr::from_str("sin(pi * x + y)").unwrap();
let mut map: HashMap<String, f64> = HashMap::new();
map.insert("x".to_owned(), 42f64);
map.insert("y".to_owned(), 24f64);
let r = expr.eval(map).unwrap();

Or maybe it could be done this way:

let mut expr = meval::Expr::from_str("sin(pi * x + y)").unwrap();
expr = expr.bind("x", 42f64).unwrap();
expr = expr.bind("y", 24f64).unwrap();
let r = expr.eval().unwrap();
rekka commented

Hi! Thanks for the suggestions. They both seem like a very good idea. I'll try to implement them if I have some time later today.

rekka commented

I think this functionality is now covered by Context.