Skip to content

Instantly share code, notes, and snippets.

@bstrie
Created February 7, 2014 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bstrie/8863386 to your computer and use it in GitHub Desktop.
Save bstrie/8863386 to your computer and use it in GitHub Desktop.
use std::iter::Unfold;
fn main() {
for fib in (0,1).unfold(next).take(20) {
println!("{}", fib);
};
}
fn next(arg: &mut (int, int)) -> Option<int> {
let (lo, hi) = *arg;
*arg = (hi, lo + hi);
Some(lo)
}
trait AnotherSillyUnfold<'a, A, St> {
fn unfold(self, f: 'a |&mut St| -> Option<A>) -> Unfold<'a, A, St>;
}
impl<'a, A, St> AnotherSillyUnfold<'a, A, St> for St {
fn unfold(self, f: 'a |&mut St| -> Option<A>) -> Unfold<'a, A, St> {
Unfold::new(self, f)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment