✨ ARCANUM RUNE DIFFICULTY: Archmage Behavioral

The Iterator

Provide a way to access the elements of an aggregate object sequentially 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 //
ability Yield a where
  yield : a -> ()
  
-- An iterator is an ability that emits values
scryArray : [a] -> '{Yield a} ()
scryArray arr _ = 
  List.map (Yield.yield) arr
  ()
  
toListHandler : '{Yield a} () -> [a]
toListHandler comp =
  h : Request (Yield a) [a] -> [a]
  h = cases
    {Yield.yield a -> resume} -> a +: (handle resume () with h)
    {_} -> []
  handle !comp with h

In Unison, iteration over unknown collections is elegantly modeled using Abilities. By defining a Yield ability, a spell can emit values one by one into the ether. The caller then provides a handler (such as toListHandler) to collect, filter, or immediately process these emitted values, fully decoupling the traversal logic from the underlying spatial representation of the artifact.

Runic Tags: #behavioral #iterator #unison #abilities #streams