Created
November 11, 2018 13:24
-
-
Save eyelash/9e24e309d43de7593d59c0284f2171b0 to your computer and use it in GitHub Desktop.
This file contains 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
struct CFor<A, B, C>(A, B, C); | |
impl<A, B, C> Iterator for CFor<A, B, C> where A: Clone, B: Fn(&A) -> bool, C: Fn(&mut A) { | |
type Item = A; | |
fn next(&mut self) -> Option<A> { | |
if !self.1(&self.0) { | |
return None; | |
} | |
let result = self.0.clone(); | |
self.2(&mut self.0); | |
Some(result) | |
} | |
} | |
fn cfor<A, B, C>(a: A, b: B, c: C) -> CFor<A, B, C> where A: Clone, B: Fn(&A) -> bool, C: Fn(&mut A) { | |
CFor(a, b, c) | |
} | |
fn main() { | |
for x in cfor(0, |x| *x < 10, |x| *x += 1) { | |
println!("{}", x); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment