Trait meval::Context [] [src]

pub trait Context {
    fn get_var(&self, _: &str) -> Option<f64> { ... }
    fn eval_func(&self, _: &str, _: &[f64]) -> Result<f64FuncEvalError> { ... }
}

Values of variables (and constants) and custom functions for substitution into an evaluated expression.

The built in context is returned by the builtin() function (or the Builtins type).

Values of variables/constants can be specified by tuples (name, value), std::collections::HashMap or std::collections::BTreeMap.

use meval::Context;

let bins = meval::builtin(); // built-ins
assert_eq!(bins.get_var("pi"), Some(std::f64::consts::PI));

let myvars = ("x", 2.);
assert_eq!(myvars.get_var("x"), Some(2f64));

let mut varmap = std::collections::HashMap::new();
varmap.insert("x", 2.);
varmap.insert("y", 3.);
assert_eq!(varmap.get_var("x"), Some(2f64));
assert_eq!(varmap.get_var("z"), None);

Custom functions can be defined using one of the CustomFunc, CustomFunc2, CustomFunc3 and CustomFuncN tuple structs.

use meval::{Context, CustomFunc2};

let cust_func = CustomFunc2("phi", |x, y| x / (y * y));

assert_eq!(cust_func.eval_func("phi", &[2., 3.]), Ok(2. / (3. * 3.)));

A Context can be built by combining other contexts:

use meval::{Context, CustomFunc2};

let bins = meval::builtin(); // built-ins
let myvars = ("x", 2.);
let cust_func = CustomFunc2("phi", |x, y| x / (y * y));

// contexts can be combined using tuples
let ctx = ((myvars, bins), cust_func); // first context has preference if there's duplicity

assert_eq!(meval::eval_str_with_context("x * pi + phi(1., 2.)", ctx).unwrap(), 2. *
            std::f64::consts::PI + 1. / (2. * 2.));

Provided Methods

fn get_var(&self, _: &str) -> Option<f64>

fn eval_func(&self, _: &str, _: &[f64]) -> Result<f64FuncEvalError>

Implementors