Skip to content

Instantly share code, notes, and snippets.

@eyelash
Created November 11, 2018 13:24
Show Gist options
  • Save eyelash/9e24e309d43de7593d59c0284f2171b0 to your computer and use it in GitHub Desktop.
Save eyelash/9e24e309d43de7593d59c0284f2171b0 to your computer and use it in GitHub Desktop.
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