rekka / meval-rs

Math expression parser and evaluation library for Rust
The Unlicense
153 stars 30 forks source link

Bind multiple variables #8

Closed Limeth closed 7 years ago

Limeth commented 8 years ago

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 8 years ago

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 7 years ago

I think this functionality is now covered by Context.