| Crates.io | tiny_tco |
| lib.rs | tiny_tco |
| version | 0.1.6 |
| created_at | 2021-01-07 19:19:41.313156+00 |
| updated_at | 2021-01-08 15:43:19.709291+00 |
| description | A tiny dirt simple no_std tail call optimization library. |
| homepage | https://github.com/10maurycy10/tiny_tco |
| repository | https://github.com/10maurycy10/tiny_tco.git |
| max_upload_size | |
| id | 333913 |
| size | 3,253 |
A tiny dirt simple no_std tail call optimization library.
The tco function returns a closure implementing a trival loop.
let mut c: TCO<A, B> = TCO::Rec(p);
loop {
match c {
TCO::Rec(i) => c = fun(i),
TCO::Ret(b) => return b,
}
}
// y is the acoumulator for the value
let fact = tco(|(x,y): (i32,i32)|
if (x == 0) {
// if we have reached 0 return computed value
TCO::Ret(y)
} else {
// reduce x by 1, and multiplyx value by x
TCO::Rec((x-1,y*x))
},
);
assert_eq!(fact((3,1)),6);