Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::iter::repeat_with;
- fn collatz(n: u64) -> u64 {
- match n % 2 {
- 0 => n / 2,
- _ => 3 * n + 1,
- }
- }
- fn iterate<F, X>(f: F, mut x: X) -> impl Iterator<Item = X>
- where
- F: Fn(X) -> X,
- X: Copy,
- {
- repeat_with(move || {
- let result = x;
- x = f(x);
- result
- })
- }
- fn main() {
- for i in iterate(collatz, 12).take_while(|&x| x != 1) {
- println!("{}", i);
- }
- }
Add Comment
Please, Sign In to add comment