R/MRatCalcpowered by RiX

RiX walkthrough 3 e

Capstone: exact inventory

Model a small inventory with arrays, maps, and sets.

The problem

A workshop needs to represent stock counts, an ordered pick list, and the distinct part names involved in an order. One collection type could hold all three, but using the collection that matches each question makes the model easier to read.

Prerequisites: Arrays, Strings and tuples, Maps, and Sets. Predict whether the repeated bolt request survives in each collection before running the example.

Build the solution

Runnable RiX

How the pieces fit

The map answers a keyed lookup, the array preserves order and repetition, and the set collapses duplicate requests. The tuple returned at the end is only a report: it groups three results without giving them new keys. Notice that collection choice is part of the program's meaning, not merely presentation.

Follow the data shapes

The same business fact appears in three shapes because each answers a different question. The event array can answer “what happened third?” The stock map can answer “how many washers remain?” The set can answer “which item names occurred at least once?” Forcing all three questions through one collection would make at least one awkward.

Duplicate bolts survive in the array but collapse in the set. That is not data loss: the set is a derived uniqueness index, and the original sequence still preserves frequency and order. The same map could later hold rational package weights or interval-valued estimates without changing its keys.

The collection shape states what the program needs to remember: order, a named count, or distinct names.

Extend the model

Once the first version works, change one input and observe which outputs change. Then add one intermediate name so the next reader can inspect the new rule without mentally executing the entire program.

Review questions

  • Why does pickList contain two bolt requests while requested contains only one bolt name?
  • Which collection answers the question “how many bolts are in stock” directly?
  • Does the original stock map change when the challenge creates a revised map?