The Iterator of the Endless Stream
Safely traversing collections and text streams without memory bloat.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Transmutation // Loopmancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// EXECUTABLE BASH SCRIPT //
#!/usr/bin/env bash
# The Aggregate (A generated stream of data)
generate_nodes() {
echo "Node_Alpha"
echo "Node_Beta"
echo "Node_Gamma"
}
# The Iterator
process_stream() {
# 'read -r' is the Iterator traversing the stream line-by-line
while IFS= read -r node; do
echo "[Iterator] Pinging $node..."
# simulate ping
sleep 0.5
done
}
# Client Code connecting Aggregate to Iterator
generate_nodes | process_stream
While arrays can be looped, the true Bash Iterator pattern is the pipeline. Using while IFS= read -r, the scriptmancer creates a memory-efficient iterator that processes infinite streams of data piece-by-piece, never requiring the full structure to manifest in RAM at once.
Runic Tags:
#bash
#iterator
#behavioral
#streams