Home / Learn

Release v0.4.0

10 Collections

Page 1 of 11

Most of a working program handles data in groups: the lines of an order, the products of a catalog, the stock of each SKU, a batch of records that arrives as JSON text. A group of values held as one value is a collection. This chapter teaches the two collections Una has today, the list and the map. It also shows how to pass a function as a value, so that one call can apply a rule to every element of a list, and how to read and write JSON. It ends with a report that a store runs at the end of each day.

What you'll build

A daily sales report. It reads the day's order lines, exported by the shop as one JSON object per line, skips the lines it cannot read, groups the rest by category, finds the best seller, and ends with a JSON summary for the accounting service:

Output
8 lines, 1 skipped
furniture: 2 units, 318.00
lighting: 8 units, 144.40
other: 2 units, 48.00
textiles: 4 units, 180.00
best seller: LAMP-01, 6 units
{"categories":4,"cents":69040,"skipped":1,"units":16}

You will learn to:

  • build lists, and visit each of their elements with for;
  • pass functions as values, and write lambdas, functions without a name;
  • transform lists with map, filter, fold and first_where, and choose between them and a loop;
  • count and group with a Map, whose keys come out in order;
  • read fields from parsed JSON, and write JSON back.

Download this chapter's packages: each step is a package under steps/, and the chapter's program is day-to-day/; run una check . and una run . in one.

Why it works this way

In many languages a collection is a shared object that anyone holding it may change. You pass a list to a function, and the function may add to it or sort it, so your list changes without a sign at the call. A lookup that finds nothing gives a special "null" value or stops the program, and its type does not say which. Parsed JSON often arrives as a loose dictionary, and a missing field shows up as a crash far from the line that read it.

Una makes each of these visible:

  1. Collections are values. append gives back a new list, and with a new map. A function that receives your list cannot change it, unless the call says so with edit (section 16.2).
  2. A change is marked where it happens. A name that changes is declared let edit, as in section 9.2, and a call that changes a map in place is written edit stock.set(...).
  3. Absence is a type. first, at, get and first_where give an Option, which you met in section 7.1, so the code says what happens when nothing is found.
  4. A function you pass keeps its promises. The type of a function parameter says whether the function may fail or print, so a rule you hand to map cannot do more than its type allows.
  5. JSON is a sum kind. A parsed value is a Json, with one variant for each kind of JSON value, and reading a field is a when and a lookup.
Key idea

A list or a map is a value. You can pass it to any function and keep using it: it holds the same elements afterwards, unless the code says, with edit, that it changes.

View the Markdown

Next:10.1 Lists