Range Over Iterators in CLIPS

JavaScript

Starting with version 1.23, the language has added support for iterators, which lets us range over pretty much anything!

Let’s look at the List type from the previous example again. In that example, we had an AllElements method that returned an array of all elements in the list. With iterators, we can do it better - as shown below.

class List {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  push(value) {
    const newElement = { value, next: null };
    if (this.tail === null) {
      this.head = newElement;
      this.tail = newElement;
    } else {
      this.tail.next = newElement;
      this.tail = newElement;
    }
  }

  *all() {
    let current = this.head;
    while (current !== null) {
      yield current.value;
      current = current.next;
    }
  }
}

All returns an iterator, which in JavaScript is a generator function.

function* genFib() {
  let a = 1, b = 1;
  while (true) {
    yield a;
    [a, b] = [b, a + b];
  }
}

Iteration doesn’t require an underlying data structure, and doesn’t even have to be finite! Here’s a function returning an iterator over Fibonacci numbers: it keeps running as long as the consumer keeps calling next().

// Example usage

const list = new List();
list.push(10);
list.push(13);
list.push(23);

// Iterate over list elements
for (let value of list.all()) {
  console.log(value);
}

// Collect all elements into an array
const all = Array.from(list.all());
console.log("all:", all);

// Generate Fibonacci numbers
for (let n of genFib()) {
  if (n >= 10) break;
  console.log(n);
}

Output:

10
13
23
all: [10, 13, 23]
1
1
2
3
5
8