✨ ARCANUM RUNE DIFFICULTY: Adept Behavioral

The Iterator Traverse

Sequentially accessing elements of a complex aggregate without exposing its underlying representation.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Scrying
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
module IteratorPattern where

open import Data.List
open import Data.Maybe

record Iterator (A : Set) : Set where
  field
    next : Maybe (A × Iterator A)
    
listIter : {A : Set} → List A → Iterator A
listIter [] = record { next = nothing }
listIter (x ∷ xs) = record { next = just (x , listIter xs) }

The Iterator Traverse

Searching a vast databanks array requires a pointer that knows how to drift through the structures. The Iterator Traverse lets you inspect each node sequentially, oblivious to whether the data lies in a List, Tree, or distant server.

The Dependent Runes

We define a co-inductive or recursive record that yields a value and a new Iterator state. Agda ensures the safety of our unwrapping.

Runic Tags: #agda #iterator #traversal