Guest User

Untitled

a guest
Oct 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. use std::iter::repeat_with;
  2.  
  3. fn collatz(n: u64) -> u64 {
  4. match n % 2 {
  5. 0 => n / 2,
  6. _ => 3 * n + 1,
  7. }
  8. }
  9.  
  10. fn iterate<F, X>(f: F, mut x: X) -> impl Iterator<Item = X>
  11. where
  12. F: Fn(X) -> X,
  13. X: Copy,
  14. {
  15. repeat_with(move || {
  16. let result = x;
  17. x = f(x);
  18. result
  19. })
  20. }
  21.  
  22. fn main() {
  23. for i in iterate(collatz, 12).take_while(|&x| x != 1) {
  24. println!("{}", i);
  25. }
  26. }
Add Comment
Please, Sign In to add comment