Vibescript Showcase

Matrix report

Pivot a grid of daily metrics into per-metric totals with transpose and zip.

Showcase
Source showcase/collections/matrix_report.vibe
# title: Matrix report
# category: Vibescript Showcase
# difficulty: Showcase
# summary: Pivot a grid of daily metrics into per-metric totals with transpose and zip.
# description: transpose swaps rows and columns so column math reads as a single map; zip re-labels the results, keeping tabular reporting free of index arithmetic.
# tags: collections, arrays, matrix
# vibe: 0.4

def column_totals(rows)
  rows.transpose.map { |column| column.sum }
end

def run
  labels = ["signups", "errors", "refunds"]
  daily = [
    [10, 3, 1],
    [12, 5, 0],
    [9, 2, 2]
  ]

  totals = column_totals(daily)

  {
    by_metric: labels.zip(totals),
    daily_totals: daily.map { |row| row.sum },
    grand_total: totals.sum
  }
end
Output
Press run to execute run from this example.