A small poetic JavaScript snippet that treats Fibonacci numbers as rhythm and structure.

Last Updated: 2025-04-25 | Tags: JavaScript, Programming, Poetry

Code on a monitor in a dim workspace.
Photo by Ferenc Almasi on Unsplash

A poetic snippet of JavaScript found in the wild. While its author remains unknown, the rhythm and structure of the logic speak for themselves.

javascript
class Symphony {
  constructor() {
    this.memo = new Map([
      [0, 0],
      [1, 1]
    ])
  }

  *fibonacci(limit = Infinity, a = 0, b = 1) {
    while (limit--) (yield a, ([a, b] = [b, a + b]))
  }

  graceful(n) {
    return this.memo.has(n)
      ? this.memo.get(n)
      : (this.memo.set(n, this.graceful(n - 1) + this.graceful(n - 2)), this.memo.get(n))
  }
}

const poetry = new Symphony()

console.log([...poetry.fibonacci(10)].map((n) => n.toString().padStart(3, ' ')).join(' → '))