The Flyweight (jq)
Compress the datascape by extracting intrinsic states into shared reference pools.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Transmutation // Compression
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
# Input with massive duplication
# [ { "type": "grunt", "hp": 100, "x": 10, "y": 20 }, ... ]
# Extracting shared (intrinsic) state to a dictionary
def extract_flyweights:
{
"dictionary": {
"grunt": { "hp": 100, "weapon": "blaster" },
"elite": { "hp": 500, "weapon": "plasma_rifle" }
},
"instances": [ .[] | { "t": .type, "x": .x, "y": .y } ]
};
# Re-hydrating via the Flyweight dictionary
def hydrate_flyweights:
.dictionary as $dict
| .instances[]
| . + $dict[.t] | del(.t);
# Usage
extract_flyweights
When the swarm grows too vast, memory collapses under the weight of redundant data. The Flyweight pattern partitions an object’s state. Intrinsic properties—those shared across a thousand entities—are banished to a singular dictionary. The JSON entities themselves retain only their extrinsic state (like coordinates), reconstructing their full form on the fly only when necessary.
Runic Tags:
#jq
#json
#transmutation
#gof