λ SCHEME RUNE DIFFICULTY: Adept Behavioral

The State

Shifting arcane forms internally without altering the outward projection.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Polymorph
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SCHEME PATTERN //
(define (make-shapeshifter)
  (let ((current-form #f))
    (define (wolf-state msg)
      (case msg ((attack) 'bite) ((shift) (set! current-form bear-state))))
    (define (bear-state msg)
      (case msg ((attack) 'maul) ((shift) (set! current-form wolf-state))))
    (set! current-form wolf-state)
    (lambda (msg) (current-form msg))))

(define shifter (make-shapeshifter))
(shifter 'attack) ; => bite
(shifter 'shift)
(shifter 'attack) ; => maul

Mutating internal state references allows the entity to dynamically swap its functional behavior without changing its identifier.

Runic Tags: #behavioral #scheme #state-machine #closures