dev/vala/EvenNumbers/main.vala

48 lines
760 B
Vala

public class EvenNumbers {
public int get(int index) {
return index * 2;
}
public bool contains(int i) {
return i % 2 == 0;
}
public string to_string() {
return "[This object enumerates even numbers]";
}
public Iterator iterator() {
return new Iterator(this);
}
public class Iterator {
private int index;
private EvenNumbers even;
public Iterator(EvenNumbers even) {
this.even = even;
}
public bool next() {
return true;
}
public int get() {
this.index++;
return this.even[this.index - 1];
}
}
}
void main() {
var even = new EvenNumbers();
stdout.printf("%d\n", even[5]);
if (4 in even) {
stdout.printf(@"$even\n");
}
foreach (var i in even) {
stdout.printf("%d\n", i);
if (i == 20) break;
}
}