Generators

BanglaCode generators let you produce values lazily using kaj* and utpadan. They return a generator object with next(), return(), and throw().

Basic Generator

kaj* count(max) {
  dhoro i = 0;
  jotokkhon (i < max) {
    utpadan i;
    i = i + 1;
  }
}

dhoro g = count(3);
dekho(g.next()); // {"value": 0, "done": mittha}
dekho(g.next()); // {"value": 1, "done": mittha}
dekho(g.next()); // {"value": 2, "done": mittha}
dekho(g.next()); // {"value": khali, "done": sotti}

Generator Methods

  • next(): Resume execution until next utpadan or completion.
  • return(value): Stop generator early and mark done.
  • throw(err): Throw an error into the generator and end it.

Early Return Example

kaj* ids() {
  utpadan 101;
  utpadan 102;
  utpadan 103;
}

dhoro g = ids();
dekho(g.next()["value"]);      // 101
dekho(g.return("stop"));       // {"value": "stop", "done": sotti}
dekho(g.next()["done"]);       // sotti