Skip to content

Instantly share code, notes, and snippets.

@takscape
Created January 21, 2015 16:26
Show Gist options
  • Save takscape/1df2484b47933da5bc70 to your computer and use it in GitHub Desktop.
Save takscape/1df2484b47933da5bc70 to your computer and use it in GitHub Desktop.
use std::iter::{Iterator, iterate};
use std::num::Int;
struct Fib<T, I> where T: Int, I: Iterator<Item=T> {
it : I
}
impl <T, I> Iterator for Fib<T, I> where
T: Int,
I: Iterator<Item=T>,
{
type Item = T;
fn next(&mut self) -> Option<T> {
self.it.next()
}
}
fn step<T>((cur, next): (T, T)) -> (T, T) where T: Int {
(next, cur + next)
}
fn fst<T>((cur, next): (T, T)) -> T where T: Int {
cur
}
fn fib<T, I>() -> Fib<T, I> where T: Int, I: Iterator<Item=T> {
let iterator : I = iterate((<T as Int>::zero(), <T as Int>::one()), step::<T>).map(fst::<T>);
Fib { it : iterator }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment