Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ where
type Product = <(Succ<U>, <(T, Succ<U>) as Mul>::Product) as Add>::Sum;
}

// Factorial

trait Factorial {
type Result;
}

// Base case: 0! = 1
impl Factorial for Zero {
type Result = Succ<Zero>;
}

impl<T> Factorial for Succ<T>
where
T: Factorial,
(Succ<T>, T::Result): Mul,
{
// n! = n * (n-1)!
type Result = <(Succ<T>, T::Result) as Mul>::Product;
}

// Division

trait GreaterThanEq {
Expand Down Expand Up @@ -229,6 +249,15 @@ mod tests {
assert_eq!(<(encode!(**), encode!(***)) as Mul>::Product::VALUE, 6);
}

#[test]
fn factorial() {
assert_eq!(<encode!() as Factorial>::Result::VALUE, 1);
assert_eq!(<encode!(*) as Factorial>::Result::VALUE, 1);
assert_eq!(<encode!(**) as Factorial>::Result::VALUE, 2);
assert_eq!(<encode!(***) as Factorial>::Result::VALUE, 6);
assert_eq!(<encode!(****) as Factorial>::Result::VALUE, 24);
}

#[test]
fn greater_than_eq() {
assert_eq!(<(encode!(), encode!()) as GreaterThanEq>::Greater::VALUE, 1);
Expand Down