tiny_tco

Crates.iotiny_tco
lib.rstiny_tco
version0.1.6
sourcesrc
created_at2021-01-07 19:19:41.313156
updated_at2021-01-08 15:43:19.709291
descriptionA tiny dirt simple no_std tail call optimization library.
homepagehttps://github.com/10maurycy10/tiny_tco
repositoryhttps://github.com/10maurycy10/tiny_tco.git
max_upload_size
id333913
size3,253
(10maurycy10)

documentation

README

tiny_tco

A tiny dirt simple no_std tail call optimization library.

how it works

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,
    }
}

how to use

// 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);
Commit count: 20

cargo fmt