Crates.io | morphism |
lib.rs | morphism |
version | 0.4.1 |
source | src |
created_at | 2014-11-22 04:28:09.423312 |
updated_at | 2016-06-22 01:06:39.430078 |
description | A structure for suspended closure composition in Rust |
homepage | |
repository | https://github.com/freebroccolo/morphism.rs |
max_upload_size | |
id | 329 |
size | 18,590 |
A structure for suspended closure composition in Rust
The motivation for Morphism
is to provide a means of composing and evaluating an unbounded (within heap constraints) number of closures without blowing the stack. In other words, Morphism
is one way to work around the lack of tail-call optimization in Rust.
Because Morphism
implements the Fn
trait, it is callable like a normal closure and can even be passed around as if it were a closure. See the second example below for an instance of using Morphism
with iterators.
Compose Morphism
with closures or another Morphism
:
let mut f = Morphism::new::<u64>();
for _ in range(0u64, 100000u64) {
f = f.tail(|x| x + 42u64);
}
let mut g = Morphism::new::<Option<u64>>();
for _ in range(0u64, 99999u64) {
g = g.tail(|x| x.map(|y| y - 42u64));
}
// type becomes Morphism<u64, (Option<u64>, bool, String)> so rebind g
let g = g
.tail(|x| (x.map(|y| y + 1000u64), String::from_str("welp")))
.tail(|(l, r)| (l.map(|y| y + 42u64), r))
.tail(|(l, r)| (l, l.is_some(), r))
.head(|x| Some(x));
let h = f.then(g);
assert_eq!(h(0u64), (Some(1084), true, String::from_str("welp")));
assert_eq!(h(1000u64), (Some(2084), true, String::from_str("welp")));
Morphism
in place of a closure when a Fn
-like is expected:use std::iter::AdditiveIterator;
let mut f = Morphism::new::<u64>();
for _ in range(0u64, 10000) {
f = f.tail(|x| x + 42);
}
// ::map treats f like any other Fn
let res = range(0u64, 100).map(f).sum();
assert_eq!(res, 42004950);
See the API documentation here.
You can install both with the following:
$ curl -s https://static.rust-lang.org/rustup.sh | sudo sh
See Installing Rust for further details.
$ cargo build ## build library and binary
$ cargo test ## run tests in ./tests
$ cargo bench ## run benchmarks in ./benches