Commit acea01ae authored by Tim McNamara's avatar Tim McNamara


parent e2c12632
This snippet of code demonstrates how to create an infinitely long `Iterator`
for a iterated function. A data structure stores some state, in this case `x`,
that is then fed into the actual function at each step.
fn logistic_map(x: f64, r: f64) -> f64 {
x * r * (1.0 - x)
struct LogisticMap {
r: f64,
x: f64,
impl Iterator for LogisticMap {
type Item = f64;
fn next(&mut self) -> Option<Self::Item> {
let new_x = logistic_map(self.x, self.r);
self.x = new_x;
fn main() {
let res: Vec<f64> = LogisticMap{r: 3.995, x: 0.5}.take(20).collect();
println!("{:#?}", res);
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment