Skip to content

Instantly share code, notes, and snippets.

@takscape
Last active August 29, 2015 14:13
Show Gist options
  • Save takscape/c9b9c493be877a5f7a06 to your computer and use it in GitHub Desktop.
Save takscape/c9b9c493be877a5f7a06 to your computer and use it in GitHub Desktop.
use std::num::Int;
use std::iter::{Iterator, iterate};
struct FibIter<'a, T:Int> {
iterator : Box<Iterator<Item=T> + 'a>
}
impl <'a, T:Int> Iterator for FibIter<'a, T> {
type Item = T;
fn next(&mut self) -> Option<T> {
self.iterator.next()
}
}
fn fib<'a, T : Int + 'a>() -> FibIter<'a, T> {
let start = (<T as Int>::zero(), <T as Int>::one());
FibIter {
iterator: Box::new(
iterate(start,
|&: (cur, nxt) : (T, T)| (nxt, cur+nxt))
.map(|&: (fst, _) : (T, T)| fst))
as Box<Iterator<Item=T>>
}
}
fn main() {
let mut it = fib::<i32>().take(20);
for v in it {
println!("{}", v);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment