soma.cell

Employee directory: records driving group_by / agg / inner_join style reporting

Demonstrates records as rows in analytical pipelines and a department roll-up.

A Soma program from the records part of the verified corpus: 54 lines, cells Employees, EmployeesTest. It passes soma check with soma 3.2.4.

How it is built

The cell Employees declares 5 signals in its face: staff() -> List, headcount(dept: String) -> Int, payroll() -> Int, avg_salary(dept: String) -> Int and raise_all(dept: String, pct: Int) -> List. Handlers: staff (line 14), headcount (line 24), payroll (line 28), avg_salary (line 32) and raise_all (line 37).

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

Generated from soma describe on this file.

The source

employees.cell

// Employee directory: records driving group_by / agg / inner_join style
// reporting. Demonstrates records as rows in analytical pipelines and a
// department roll-up.

cell Employees {
    face {
        signal staff() -> List
        signal headcount(dept: String) -> Int
        signal payroll() -> Int
        signal avg_salary(dept: String) -> Int
        signal raise_all(dept: String, pct: Int) -> List
    }

    on staff() {
        return list(
            Emp { name: "Ada",   dept: "eng",   salary: 150 },
            Emp { name: "Bea",   dept: "eng",   salary: 130 },
            Emp { name: "Cyd",   dept: "sales", salary: 90  },
            Emp { name: "Dot",   dept: "sales", salary: 110 },
            Emp { name: "Eli",   dept: "eng",   salary: 170 }
        )
    }

    on headcount(dept: String) {
        return staff() |> count(e => e.dept == dept)
    }

    on payroll() {
        return sum(staff() |> pluck("salary"))
    }

    on avg_salary(dept: String) {
        let rows = staff() |> filter(e => e.dept == dept)
        return avg(rows |> pluck("salary"))
    }

    on raise_all(dept: String, pct: Int) {
        return staff()
            |> filter(e => e.dept == dept)
            |> map(e => e |> with("salary", e.salary + floor(e.salary * pct / 100)))
    }
}

cell test EmployeesTest {
    rules {
        assert headcount("eng") == 3
        assert payroll() == 650
        assert avg_salary("eng") == 150
        assert avg_salary("sales") == 100
        assert raise_all("sales", 10)[0].salary == 99
        assert len(raise_all("eng", 5)) == 3
    }
}

What the toolchain says

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

$ soma check employees.cell
✓ All checks passed.
$ soma test employees.cell
test EmployeesTest ...
  ✓ assert headcount("eng") == 3
  ✓ assert payroll() == 650
  ✓ assert avg_salary("eng") == 150
  ✓ assert avg_salary("sales") == 100
  ✓ assert raise_all("sales", 10)[0].salary == 99
  ✓ assert len(raise_all("eng", 5)) == 3

6 tests: 6 passed, 0 failed
$ soma verify employees.cell
Verifying employees.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

avg, count, filter, floor, len, list, map, pluck, sum, with

Source file: /corpus/records/employees.cell · examples/corpus/records/employees.cell