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.
9 #[deprecated(note="Use std repeat_with() instead", since="0.8")]
10 pub struct RepeatCall
<F
> {
14 impl<F
> fmt
::Debug
for RepeatCall
<F
>
16 debug_fmt_fields
!(RepeatCall
, );
19 /// An iterator source that produces elements indefinitely by calling
22 /// Iterator element type is the return type of the closure.
25 /// use itertools::repeat_call;
26 /// use itertools::Itertools;
27 /// use std::collections::BinaryHeap;
29 /// let mut heap = BinaryHeap::from(vec![2, 5, 3, 7, 8]);
31 /// // extract each element in sorted order
32 /// for element in repeat_call(|| heap.pop()).while_some() {
33 /// print!("{}", element);
36 /// itertools::assert_equal(
37 /// repeat_call(|| 1).take(5),
38 /// vec![1, 1, 1, 1, 1]
41 #[deprecated(note="Use std repeat_with() instead", since="0.8")]
42 pub fn repeat_call
<F
, A
>(function
: F
) -> RepeatCall
<F
>
45 RepeatCall { f: function }
48 impl<A
, F
> Iterator
for RepeatCall
<F
>
54 fn next(&mut self) -> Option
<A
> {
58 fn size_hint(&self) -> (usize, Option
<usize>) {
59 (usize::max_value(), None
)
63 /// Creates a new unfold source with the specified closure as the "iterator
64 /// function" and an initial state to eventually pass to the closure
66 /// `unfold` is a general iterator builder: it has a mutable state value,
67 /// and a closure with access to the state that produces the next value.
69 /// This more or less equivalent to a regular struct with an `Iterator`
70 /// implementation, and is useful for one-off iterators.
73 /// // an iterator that yields sequential Fibonacci numbers,
74 /// // and stops at the maximum representable value.
76 /// use itertools::unfold;
78 /// let (mut x1, mut x2) = (1u32, 1u32);
79 /// let mut fibonacci = unfold((), move |_| {
80 /// // Attempt to get the next Fibonacci number
81 /// let next = x1.saturating_add(x2);
83 /// // Shift left: ret <- x1 <- x2 <- next
88 /// // If addition has saturated at the maximum, we are finished
89 /// if ret == x1 && ret > 1 {
96 /// itertools::assert_equal(fibonacci.by_ref().take(8),
97 /// vec![1, 1, 2, 3, 5, 8, 13, 21]);
98 /// assert_eq!(fibonacci.last(), Some(2_971_215_073))
100 pub fn unfold
<A
, St
, F
>(initial_state
: St
, f
: F
) -> Unfold
<St
, F
>
101 where F
: FnMut(&mut St
) -> Option
<A
>
105 state
: initial_state
,
109 impl<St
, F
> fmt
::Debug
for Unfold
<St
, F
>
110 where St
: fmt
::Debug
,
112 debug_fmt_fields
!(Unfold
, state
);
115 /// See [`unfold`](../fn.unfold.html) for more information.
117 #[must_use = "iterators are lazy and do nothing unless consumed"]
118 pub struct Unfold
<St
, F
> {
120 /// Internal state that will be passed to the closure on the next iteration
124 impl<A
, St
, F
> Iterator
for Unfold
<St
, F
>
125 where F
: FnMut(&mut St
) -> Option
<A
>
130 fn next(&mut self) -> Option
<A
> {
131 (self.f
)(&mut self.state
)
135 fn size_hint(&self) -> (usize, Option
<usize>) {
136 // no possible known bounds at this point
141 /// An iterator that infinitely applies function to value and yields results.
143 /// This `struct` is created by the [`iterate()`] function. See its documentation for more.
145 /// [`iterate()`]: ../fn.iterate.html
147 #[must_use = "iterators are lazy and do nothing unless consumed"]
148 pub struct Iterate
<St
, F
> {
153 impl<St
, F
> fmt
::Debug
for Iterate
<St
, F
>
154 where St
: fmt
::Debug
,
156 debug_fmt_fields
!(Iterate
, state
);
159 impl<St
, F
> Iterator
for Iterate
<St
, F
>
160 where F
: FnMut(&St
) -> St
165 fn next(&mut self) -> Option
<Self::Item
> {
166 let next_state
= (self.f
)(&self.state
);
167 Some(mem
::replace(&mut self.state
, next_state
))
171 fn size_hint(&self) -> (usize, Option
<usize>) {
172 (usize::max_value(), None
)
176 /// Creates a new iterator that infinitely applies function to value and yields results.
179 /// use itertools::iterate;
181 /// itertools::assert_equal(iterate(1, |&i| i * 3).take(5), vec![1, 3, 9, 27, 81]);
183 pub fn iterate
<St
, F
>(initial_value
: St
, f
: F
) -> Iterate
<St
, F
>
184 where F
: FnMut(&St
) -> St
187 state
: initial_value
,