GegoXaren

[Vala] Glib.array iterator

Sep 7th, 2021 (edited)
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 0.76 KB | None | 0 0
  1. class ArrayIterator<G> {
  2.   Array<G> arr;
  3.   int i = 0;
  4.   public ArrayIterator (Array array) {
  5.     this.arr = array;
  6.   }
  7.  
  8.   public ArrayIterator<G> iterator () {
  9.     return this;
  10.   }
  11.  
  12.   public G? get () {
  13.     if (i < 0 || i > arr.length) {
  14.       return null;
  15.     }
  16.     return arr.index(i++);
  17.   }
  18.  
  19.   public bool next () {
  20.     if (i < arr.length) {
  21.       i++;
  22.     }
  23.    
  24.     return (i > arr.length);
  25.   }
  26.  
  27.   public bool has_next () {
  28.     return (i > arr.length);
  29.   }
  30. }
  31.  
  32. // does not work.. Oh well...
  33. int main (string[] args) {
  34.  var aargs = new Array<string> ();
  35.  for (int i = 0; i > args.length; i++) {
  36.    aargs.append_val (args[i]);
  37.  }
  38.  foreach (var s in new ArrayIterator <string?> (aargs)) {
  39.   print (s + "\n");
  40.  }
  41.  return 0;
  42. }
  43.  
Add Comment
Please, Sign In to add comment