improve function handling

This commit is contained in:
Simon Gardling
2022-02-22 10:51:23 -05:00
parent 30a8c28beb
commit 7a00ed62f6
2 changed files with 46 additions and 19 deletions

View File

@@ -1,4 +1,5 @@
use wasm_bindgen::prelude::*;
use meval::Expr;
pub type DrawResult<T> = Result<T, Box<dyn std::error::Error>>;
@@ -87,6 +88,36 @@ pub fn add_asterisks(function_in: String) -> String {
output_string
}
pub struct Function {
function: Box<dyn Fn(f64) -> f64>,
func_str: String
}
impl Function {
pub fn from_string(func_str: String) -> Self {
let expr: Expr = func_str.parse().unwrap();
let func = expr.bind("x").unwrap();
Self {
function: Box::new(func),
func_str
}
}
#[inline]
pub fn run(&self, x: f32) -> f32 {
(self.function)(x as f64) as f32
}
pub fn str_compare(&self, other_string: String) -> bool {
self.func_str == other_string
}
pub fn get_string(&self) -> String {
self.func_str.clone()
}
}
/// Result of screen to chart coordinates conversion.
#[wasm_bindgen]
pub struct Point {