mirror of
https://github.com/kmc7468/cs220.git
synced 2025-12-15 06:28:46 +00:00
45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
//! Generators
|
|
//!
|
|
//! HINT: Look at the `generator_grade.rs` file to see how the generator is used.
|
|
|
|
/// Yielded value. It can be either a value or a stop signal.
|
|
enum Yielded<T> {
|
|
Value(T),
|
|
Stop,
|
|
}
|
|
|
|
/// Generator
|
|
/// - kk
|
|
/// - You can call `next()` method to get the next value.
|
|
/// - The generator should stop when it yields `Yielded::Stop`.
|
|
///
|
|
/// Reference:
|
|
/// - [Python generator](https://python-reference.readthedocs.io/en/latest/docs/generator/)
|
|
#[allow(missing_debug_implementations)]
|
|
pub struct Generator<T, S> {
|
|
state: S,
|
|
f: fn(&mut S) -> Yielded<T>,
|
|
}
|
|
|
|
impl<T, S> Iterator for Generator<T, S> {
|
|
type Item = T;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
/// Returns a generator that yields fibonacci numbers.
|
|
///
|
|
/// HINT: Consult <https://en.wikipedia.org/wiki/Fibonacci_sequence>
|
|
pub fn fib_generator(first: usize, second: usize) -> Generator<usize, (usize, usize)> {
|
|
todo!()
|
|
}
|
|
|
|
/// Returns a generator that yields collatz numbers.
|
|
///
|
|
/// HINT: Consult <https://en.wikipedia.org/wiki/Collatz_conjecture>
|
|
pub fn collatz_conjecture(start: usize) -> Generator<usize, usize> {
|
|
todo!()
|
|
}
|