Last active
August 29, 2015 14:13
-
-
Save takscape/c9b9c493be877a5f7a06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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