🌐 REASONML PACT DIFFICULTY: Adept Behavioral

Iterator in ReasonML

Lazily traversing ethereal sequences.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Scrying
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// REASON PATTERN //
type iterator('a) = unit => option('a);

let rec makeRange = (start, stop): iterator(int) => {
  let curr = ref(start);
  () => {
    if (curr^ <= stop) {
      let v = curr^;
      curr := curr^ + 1;
      Some(v);
    } else {
      None;
    }
  };
};

Iterators are functions closing over state, emitting Some(val) until the stream of magical energy is exhausted (None).

Runic Tags: #reason #iterator #behavioral