πŸͺ OCAML METAMAGIC DIFFICULTY: Archmage Behavioral

The Iterator

Traversing boundless magical dimensions lazily.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Dimension Traversal
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// OCAML PATTERN //
type 'a seq = Cons of 'a * (unit -> 'a seq) | Nil

let rec infinite_mana n = Cons (n, fun () -> infinite_mana (n + 10))

let rec take n seq =
  if n = 0 then []
  else match seq with
  | Nil -> []
  | Cons (x, next) -> x :: take (n - 1) (next ())

let mana_crystals = take 3 (infinite_mana 10)

The Iterator is realized via lazy sequences. We can iterate over boundless magical dimensions without exhausting the material plane’s memory.

Runic Tags: #Caml Metamagic #Lazy Sequences #OCaml