soma.cell

Pipeline over typed Record literals (Name { field: v }) instead of map()

Records carry a _type tag and support dot-mutation. Demonstrates that the data builtins (filter_by, sort_by, agg, pluck) work on records too.

A Soma program from the data part of the verified corpus: 46 lines, cells RecordPipeline, RecordPipelineTests. It passes soma check with soma 3.2.4.

How it is built

The cell RecordPipeline has no face and no memory. Handlers: staff (line 6), seniors (line 17), payroll_by_dept (line 19), top_earner (line 25) and run (line 27).

The file also holds 1 test cell (RecordPipelineTests) with 5 assertions, run by soma test.

Generated from soma describe on this file.

The source

record_pipeline.cell

// Pipeline over typed Record literals (Name { field: v }) instead of map().
// Records carry a _type tag and support dot-mutation. Demonstrates that the
// data builtins (filter_by, sort_by, agg, pluck) work on records too.

cell RecordPipeline {
    on staff() {
        return list(
            Person { name: "Mara", dept: "Eng",   salary: 130, level: 5 },
            Person { name: "Theo", dept: "Eng",   salary: 95,  level: 3 },
            Person { name: "Lena", dept: "Sales", salary: 80,  level: 2 },
            Person { name: "Omar", dept: "Sales", salary: 110, level: 4 },
            Person { name: "Priya", dept: "Eng", salary: 145, level: 6 },
            Person { name: "Sven", dept: "Ops",   salary: 70,  level: 2 }
        )
    }

    on seniors() { return staff() |> filter_by("level", ">=", 4) |> sort_by("salary", "desc") }

    on payroll_by_dept() {
        return staff()
            |> agg("dept", "salary:sum", "salary:avg")
            |> sort_by("salary_sum", "desc")
    }

    on top_earner() { return max_by(staff(), "salary") }

    on run() {
        for r in payroll_by_dept() { print("{r.dept}: total {r.salary_sum}, avg {r.salary_avg}") }
        return top_earner().name
    }
}

cell test RecordPipelineTests {
    rules {
        // records are recognized as their type
        assert is_a(Person { name: "x", dept: "d", salary: 1, level: 1 }, "Person") == true
        // 3 seniors (level >= 4): Priya, Mara, Omar
        assert len(seniors()) == 3
        assert seniors()[0].name == "Priya"
        // Eng payroll = 130 + 95 + 145 = 370
        assert (payroll_by_dept() |> filter_by("dept", "=", "Eng"))[0].salary_sum == 370
        // overall top earner is Priya
        assert top_earner().name == "Priya"
    }
}

What the toolchain says

Run on a fresh copy of the program at build time; the outputs are pasted unedited.

$ soma check record_pipeline.cell
✓ All checks passed.
$ soma test record_pipeline.cell
test RecordPipelineTests ...
  ✓ assert is_a(Person { name: "x", dept: "d", salary: 1, level: 1 }, "Person") == true
  ✓ assert len(seniors()) == 3
  ✓ assert seniors()[0].name == "Priya"
  ✓ assert (payroll_by_dept() |> filter_by("dept", "=", "Eng"))[0].salary_sum == 370
  ✓ assert top_earner().name == "Priya"

5 tests: 5 passed, 0 failed
$ soma verify record_pipeline.cell
Verifying record_pipeline.cell...
No state machine in this program: nothing to prove beyond `soma check` (invariants and lifecycles are what verify proves).
VERIFY OK (vacuous — no state machine)

Builtins used

agg, filter_by, is_a, len, list, max_by, print, sort_by

Source file: /corpus/data/record_pipeline.cell · examples/corpus/data/record_pipeline.cell