Expand description
§unfold
The unfold function takes as input a function f and an initial i value and returns a list defined as [i, f(i), f(f(i)), …].
This library defines Unfold
a struct
that implements the unfold function as an endless
iterator.
§Quick Start
To use Unfold
is quite simple. The user calls
the new function providing a function as first argument
and the initial value as second argument.
An Unfold
instance can be then used as any iterator,
but don’t forget: Unfold never ends, it must be stopped.
Here an example:
use unfold::Unfold;
// Create a vector containing the first 5 numbers from the Fibonacci
// series
let fibonacci_numbers: Vec<u64> = Unfold::new(|(a, b)| (b, a + b), (0, 1))
.map(|(a, _)| a)
.take(5) //Unfold iterator never stops.
.collect();
assert_eq!(vec![0, 1, 1, 2, 3], fibonacci_numbers);
Structs§
- Unfold
- Define an endless unfold iterator
Functions§
- unfold
- This function it is a simple front-end to Unfold::new : allows the user to easily create a new Unfold iterator
- unfold_
count - This function create an unfold iterator that stops at count iterations. Note this is a standard lazy iterator
- unfold_
nth - This function create an unfold iterator and returns its index-th item.
- unfold_
vector - This function create an unfold iterator and collects its first len items into a vector