pub enum Op {
Add,
Subtract,
Multiply,
Divide,
Pow,
}
pub trait Operation {
fn name(&self) -> &'static str;
fn apply(&self, a: f64, b: f64) -> f64;
}
pub struct AddOp;
pub struct MulOp;
pub struct DivOp;
impl Operation for AddOp {
fn name(&self) -> &'static str { "add" }
fn apply(&self, a: f64, b: f64) -> f64 { a + b }
}
impl Operation for MulOp {
fn name(&self) -> &'static str { "mul" }
fn apply(&self, a: f64, b: f64) -> f64 { a * b }
}
impl Operation for DivOp {
fn name(&self) -> &'static str { "div" }
fn apply(&self, a: f64, b: f64) -> f64 { a / b }
}
pub fn add(a: f64, b: f64) -> f64 { a + b }
pub fn subtract(a: f64, b: f64) -> f64 { a - b }
pub fn multiply(a: f64, b: f64) -> f64 { a * b }
pub fn divide(a: f64, b: f64) -> f64 {
if b == 0.0 { panic!("division by zero") }
a / b
}