1 //! Iterators that are sources (produce elements from parameters,
2 //! not from another iterator).
8 /// See [`repeat_call`](../fn.repeat_call.html) for more information.
10 #[deprecated(note="Use std repeat_with() instead", since="0.8")]
11 pub struct RepeatCall
<F
> {
15 impl<F
> fmt
::Debug
for RepeatCall
<F
>
17 debug_fmt_fields
!(RepeatCall
, );
20 /// An iterator source that produces elements indefinitely by calling
23 /// Iterator element type is the return type of the closure.
26 /// use itertools::repeat_call;
27 /// use itertools::Itertools;
28 /// use std::collections::BinaryHeap;
30 /// let mut heap = BinaryHeap::from(vec![2, 5, 3, 7, 8]);
32 /// // extract each element in sorted order
33 /// for element in repeat_call(|| heap.pop()).while_some() {
34 /// print!("{}", element);
37 /// itertools::assert_equal(
38 /// repeat_call(|| 1).take(5),
39 /// vec![1, 1, 1, 1, 1]
42 #[deprecated(note="Use std repeat_with() instead", since="0.8")]
43 pub fn repeat_call
<F
, A
>(function
: F
) -> RepeatCall
<F
>
46 RepeatCall { f: function }
49 impl<A
, F
> Iterator
for RepeatCall
<F
>
55 fn next(&mut self) -> Option
<A
> {
59 fn size_hint(&self) -> (usize, Option
<usize>) {
60 (usize::max_value(), None
)
64 /// Creates a new unfold source with the specified closure as the "iterator
65 /// function" and an initial state to eventually pass to the closure
67 /// `unfold` is a general iterator builder: it has a mutable state value,
68 /// and a closure with access to the state that produces the next value.
70 /// This more or less equivalent to a regular struct with an `Iterator`
71 /// implementation, and is useful for one-off iterators.
74 /// // an iterator that yields sequential Fibonacci numbers,
75 /// // and stops at the maximum representable value.
77 /// use itertools::unfold;
79 /// let (mut x1, mut x2) = (1u32, 1u32);
80 /// let mut fibonacci = unfold((), move |_| {
81 /// // Attempt to get the next Fibonacci number
82 /// let next = x1.saturating_add(x2);
84 /// // Shift left: ret <- x1 <- x2 <- next
89 /// // If addition has saturated at the maximum, we are finished
90 /// if ret == x1 && ret > 1 {
97 /// itertools::assert_equal(fibonacci.by_ref().take(8),
98 /// vec![1, 1, 2, 3, 5, 8, 13, 21]);
99 /// assert_eq!(fibonacci.last(), Some(2_971_215_073))
101 pub fn unfold
<A
, St
, F
>(initial_state
: St
, f
: F
) -> Unfold
<St
, F
>
102 where F
: FnMut(&mut St
) -> Option
<A
>
106 state
: initial_state
,
110 impl<St
, F
> fmt
::Debug
for Unfold
<St
, F
>
111 where St
: fmt
::Debug
,
113 debug_fmt_fields
!(Unfold
, state
);
116 /// See [`unfold`](../fn.unfold.html) for more information.
118 #[must_use = "iterators are lazy and do nothing unless consumed"]
119 pub struct Unfold
<St
, F
> {
121 /// Internal state that will be passed to the closure on the next iteration
125 impl<A
, St
, F
> Iterator
for Unfold
<St
, F
>
126 where F
: FnMut(&mut St
) -> Option
<A
>
131 fn next(&mut self) -> Option
<A
> {
132 (self.f
)(&mut self.state
)
136 fn size_hint(&self) -> (usize, Option
<usize>) {
137 // no possible known bounds at this point
142 /// An iterator that infinitely applies function to value and yields results.
144 /// This `struct` is created by the [`iterate()`] function. See its documentation for more.
146 /// [`iterate()`]: ../fn.iterate.html
148 #[must_use = "iterators are lazy and do nothing unless consumed"]
149 pub struct Iterate
<St
, F
> {
154 impl<St
, F
> fmt
::Debug
for Iterate
<St
, F
>
155 where St
: fmt
::Debug
,
157 debug_fmt_fields
!(Iterate
, state
);
160 impl<St
, F
> Iterator
for Iterate
<St
, F
>
161 where F
: FnMut(&St
) -> St
166 fn next(&mut self) -> Option
<Self::Item
> {
167 let next_state
= (self.f
)(&self.state
);
168 Some(mem
::replace(&mut self.state
, next_state
))
172 fn size_hint(&self) -> (usize, Option
<usize>) {
173 (usize::max_value(), None
)
177 /// Creates a new iterator that infinitely applies function to value and yields results.
180 /// use itertools::iterate;
182 /// itertools::assert_equal(iterate(1, |&i| i * 3).take(5), vec![1, 3, 9, 27, 81]);
184 pub fn iterate
<St
, F
>(initial_value
: St
, f
: F
) -> Iterate
<St
, F
>
185 where F
: FnMut(&St
) -> St
188 state
: initial_value
,