pub struct Xpr<T>(/* private fields */);
Expand description
An expression. Xpr
together with the Fold
trait are at the heart of this crate.
The nested type is a specific struct representing the operation, e.g. ops::Add
.
Xpr
instances should be instantiated via the Xpr::new
method, which creates
Xpr<ops::Term>
leaf expressions. The other variants are constructed from them by
applying operations to the leaf expressions.
use xpr::*;
let x = Xpr::new(5);
let y = Xpr::new(1);
let z = x * y;
//type of z is xpr::Xpr<xpr::ops::Mul<xpr::Xpr<xpr::ops::Term<{integer}>>, xpr::Xpr<xpr::ops::Term<{integer}>>>>
Implementations§
Source§impl<T> Xpr<Term<T>>
impl<T> Xpr<Term<T>>
Sourcepub const fn new(t: T) -> Self
pub const fn new(t: T) -> Self
creates a new leaf expression.
Examples found in repository?
examples/fortitwoify.rs (line 12)
11 fn fold(&mut self, _: &Term<i32>) -> Self::Output {
12 Xpr::new(42)
13 }
14}
15
16fn main() {
17 // create a new expression representing a chained addition
18 let x = Xpr::new(10) + Xpr::new(15) + Xpr::new(17);
19 println!("x = {:?}", x);
20
21 println!("x evaluates to {}.", x.eval());
22 assert_eq!(x.eval(), 42);
23
24 // let's use the Fortytwoify folder
25 println!("\n>>> Fortitwoify x <<<\n");
26 let x = Fortytwoify.fold(&x);
27 println!("x = {:?}", x);
28
29 println!("x evaluates to {}.", x.eval());
30 assert_eq!(x.eval(), 126);
31}
More examples
examples/substitution.rs (line 13)
12 fn fold(&mut self, &Term(x): &Term<i32>) -> Self::Output {
13 Xpr::new(x - 42) + Xpr::new(42)
14 }
15}
16
17fn main() {
18 // create a new expression representing a chained addition
19 let x = Xpr::new(10) + Xpr::new(15) + Xpr::new(17);
20 println!("x = {:?}", x);
21
22 println!("x evaluates to {}.", x.eval());
23 assert_eq!(x.eval(), 42);
24
25 // let's use the Substitution folder
26 println!("Substitution fold...");
27 let x = Substitution.fold(&x);
28 println!("x = {:?}", x);
29
30 println!("x evaluates to {}.", x.eval());
31 assert_eq!(x.eval(), 42);
32}
examples/evaluator.rs (line 17)
15fn main() {
16 // create a new expression representing a chained addition
17 let x = Xpr::new(10) + Xpr::new(15) + Xpr::new(17);
18 println!("x = {:?}", x);
19
20 // use the Evaluator to evaluate the expression
21 let res = Evaluator.fold(&x);
22
23 println!("x evaluates to {}.", res);
24 assert_eq!(res, 42);
25
26 // note that `Xpr::eval` is syntactic sugar around a similar generic
27 // Evaluator struct
28 assert_eq!(res, x.eval());
29}
Source§impl<U> Xpr<U>
impl<U> Xpr<U>
Sourcepub fn eval<T>(&self) -> <Self as Foldable<Evaluator<T>>>::Output
pub fn eval<T>(&self) -> <Self as Foldable<Evaluator<T>>>::Output
evaluates the expression by unwrapping all terminals and applying the operations
in the expression. It is synactic sugar for folding the expression with [Evaluator
].
Examples found in repository?
examples/evaluator.rs (line 28)
15fn main() {
16 // create a new expression representing a chained addition
17 let x = Xpr::new(10) + Xpr::new(15) + Xpr::new(17);
18 println!("x = {:?}", x);
19
20 // use the Evaluator to evaluate the expression
21 let res = Evaluator.fold(&x);
22
23 println!("x evaluates to {}.", res);
24 assert_eq!(res, 42);
25
26 // note that `Xpr::eval` is syntactic sugar around a similar generic
27 // Evaluator struct
28 assert_eq!(res, x.eval());
29}
More examples
examples/substitution.rs (line 22)
17fn main() {
18 // create a new expression representing a chained addition
19 let x = Xpr::new(10) + Xpr::new(15) + Xpr::new(17);
20 println!("x = {:?}", x);
21
22 println!("x evaluates to {}.", x.eval());
23 assert_eq!(x.eval(), 42);
24
25 // let's use the Substitution folder
26 println!("Substitution fold...");
27 let x = Substitution.fold(&x);
28 println!("x = {:?}", x);
29
30 println!("x evaluates to {}.", x.eval());
31 assert_eq!(x.eval(), 42);
32}
examples/fortitwoify.rs (line 21)
16fn main() {
17 // create a new expression representing a chained addition
18 let x = Xpr::new(10) + Xpr::new(15) + Xpr::new(17);
19 println!("x = {:?}", x);
20
21 println!("x evaluates to {}.", x.eval());
22 assert_eq!(x.eval(), 42);
23
24 // let's use the Fortytwoify folder
25 println!("\n>>> Fortitwoify x <<<\n");
26 let x = Fortytwoify.fold(&x);
27 println!("x = {:?}", x);
28
29 println!("x evaluates to {}.", x.eval());
30 assert_eq!(x.eval(), 126);
31}
Trait Implementations§
impl<T: Copy> Copy for Xpr<T>
Auto Trait Implementations§
impl<T> Freeze for Xpr<T>where
T: Freeze,
impl<T> RefUnwindSafe for Xpr<T>where
T: RefUnwindSafe,
impl<T> Send for Xpr<T>where
T: Send,
impl<T> Sync for Xpr<T>where
T: Sync,
impl<T> Unpin for Xpr<T>where
T: Unpin,
impl<T> UnwindSafe for Xpr<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more