Vibescript Showcase

Statement grid

Total a period-by-category grid of money values by pivoting rows into columns with transpose.

Showcase
Source showcase/finance/statement_grid.vibe
# title: Statement grid
# category: Vibescript Showcase
# difficulty: Showcase
# summary: Total a period-by-category grid of money values by pivoting rows into columns with transpose.
# description: Money values keep their currency through the whole rollup, and transpose turns per-category column totals into a single map, so financial summaries stay exact and readable.
# tags: finance, money, collections
# vibe: 0.4

def column_totals(rows)
  rows.transpose.map { |column| column.sum(money("0.00 USD")) }
end

def run
  categories = ["subscriptions", "usage", "support"]
  weeks = [
    [money("1200.00 USD"), money("340.00 USD"), money("90.00 USD")],
    [money("1250.00 USD"), money("410.00 USD"), money("75.00 USD")],
    [money("1180.00 USD"), money("505.00 USD"), money("120.00 USD")]
  ]

  totals = column_totals(weeks)

  {
    by_category: categories.zip(totals),
    weekly_totals: weeks.map { |week| week.sum(money("0.00 USD")) },
    grand_total: totals.sum(money("0.00 USD"))
  }
end
Output
Press run to execute run from this example.