🚀 RACKET METAMAGIC DIFFICULTY: Adept Creational

The Singleton (Racket)

A solitary, ancient rune that exists only once across the entire runtime.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Singleton
ARCANUM SCHOOL: Enchantment // Monolith
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// RACKET PATTERN //
#lang racket

(define get-mana-pool
  (let ([pool-instance #f])
    (λ ()
      (unless pool-instance
        (displayln "Initializing the Ancient Mana Pool...")
        (set! pool-instance (make-hash)))
      pool-instance)))

(define pool1 (get-mana-pool))
(define pool2 (get-mana-pool))

(hash-set! pool1 'crystal 100)
(printf "Pool 2 Crystal Count: ~a\n" (hash-ref pool2 'crystal))

The Singleton

A Singleton is an entity of absolute uniqueness. Through the use of lexical scope and set!, we bind a single instance of a hash map to the closure of get-mana-pool. No matter how many times the function is invoked, the same underlying magical reservoir is tapped, making it an excellent anchor for global arcane state.

Runic Tags: #racket #creational #singleton #parenthetical-evolution