I want to chain functions, but let them follow a section,
example
sum(2,3);
sum(2,3).subtract(1);
sum(2,3).subtract(1).pow(2);
sum(2,3).subtract(1).pow(2).divede(2);
Here is my proof of concept
With this it works, but how can I improve it?
pub fn sum(a: u32, b: u32) -> Number {
Number { result: a + b }
}
#[derive(Debug)]
pub struct Number {
pub result: u32,
}
impl Number {
pub fn subtract(&self, rhs: u32) -> Pow {
Pow {
result: self.result - rhs,
}
}
}
#[derive(Debug)]
pub struct Subtract {
pub result: u32,
}
impl Subtract {
pub fn subtract(&self, rhs: u32) -> Self {
Self {
result: self.result - rhs,
}
}
}
#[derive(Debug)]
pub struct Pow {
pub result: u32,
}
impl Pow {
pub fn pow(&self, rhs: u32) -> Divide {
Divide {
result: self.result.pow(rhs)
}
}
}
#[derive(Debug)]
pub struct Divide {
pub result: u32,
}
impl Divide {
pub fn divide(&self, rhs: u32) -> Self {
Self {
result: self.result / rhs,
}
}
}
fn main() {
let opr = sum(2, 3).subtract(1).pow(2).divide(2);
println!("opr: {:?}", opr.result)
}
Here is the working example