λ SCHEME RUNE DIFFICULTY: Adept Behavioral

The Iterator

Traversing infinite astral planes using lazy streams.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Scrying
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SCHEME PATTERN //
(define (make-astral-iterator lst)
  (let ((current lst))
    (lambda ()
      (if (null? current)
          'end-of-plane
          (let ((val (car current)))
            (set! current (cdr current))
            val)))))

(define iter (make-astral-iterator '(star nebula void)))
(iter) ; => star

A stateful closure encapsulates the traversal of planar structures, emitting elements sequentially upon each invocation.

Runic Tags: #behavioral #scheme #stateful-closures #streams