✨ GLEAM ALCHEMY DIFFICULTY: Apprentice Structural

The Flyweight

Efficient structural sharing for repetitive arcana.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Transmutation // Essence Sharing
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// GLEAM PATTERN //
import gleam/dict.{type Dict}

pub type ParticleType {
  Spark(color: String, heat: Int)
}

pub type Particle {
  Particle(x: Int, y: Int, shared_type: ParticleType)
}

pub fn spawn_particles(ptype: ParticleType, count: Int) -> List(Particle) {
  // Due to BEAM immutable data structures, `ptype` is shared in memory
  // across all Particle instances automatically.
  gleam/list.repeat(Particle(0, 0, ptype), count)
}

The Flyweight

On the BEAM, structural sharing is free. Shared references cost virtually zero memory.

Runic Tags: #transmutation #flyweight #gleam