soma.cell

Game leaderboard: rank players by score, award medals, compute gaps

Demonstrates sort_by("score","desc") -> top, pluck, max_by/min_by, and per-row derived fields via map(... with ...). Records built with map().

A Soma program from the data part of the verified corpus: 60 lines, cells Leaderboard, LeaderboardTests. It passes soma check with soma 3.2.4.

How it is built

The cell Leaderboard has no face and no memory. Handlers: players (line 6), ranked (line 18), podium (line 20), champion (line 22), with_ranks (line 25) and run (line 36).

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

Generated from soma describe on this file.

The source

leaderboard.cell

// Game leaderboard: rank players by score, award medals, compute gaps.
// Demonstrates sort_by("score","desc") -> top, pluck, max_by/min_by, and
// per-row derived fields via map(... with ...). Records built with map().

cell Leaderboard {
    on players() {
        return list(
            map("name", "nova",   "score", 4200, "wins", 31, "country", "PT"),
            map("name", "blaze",  "score", 5100, "wins", 44, "country", "BR"),
            map("name", "kira",   "score", 3800, "wins", 22, "country", "JP"),
            map("name", "echo",   "score", 5100, "wins", 39, "country", "US"),
            map("name", "vex",    "score", 2900, "wins", 17, "country", "DE"),
            map("name", "rune",   "score", 4750, "wins", 28, "country", "PT")
        )
    }

    // Ranked descending; ties keep stable order.
    on ranked() { return players() |> sort_by("score", "desc") }

    on podium() { return ranked() |> top(3) |> pluck("name") }

    on champion() { return max_by(players(), "score") }

    // Attach a rank index 1..n as a new field on each row.
    on with_ranks() {
        let rows = ranked()
        let out = list()
        let i = 0
        while i < len(rows) {
            out = out.push(rows[i] |> with("rank", i + 1))
            i += 1
        }
        return out
    }

    on run() {
        for r in with_ranks() {
            print("#{r.rank} {r.name} - {r.score}")
        }
        return podium()
    }
}

cell test LeaderboardTests {
    rules {
        // highest score is 5100, appears twice; max_by returns one of them
        assert champion().score == 5100
        // podium is a list of names; top-ranked name is blaze or echo (tie at 5100)
        assert ranked()[0].score == 5100
        assert len(podium()) == 3
        // lowest scorer is vex
        assert min_by(players(), "score").name == "vex"
        // rank #1 has the top score
        assert with_ranks()[0].rank == 1
        assert with_ranks()[5].rank == 6
        // there are 6 players
        assert len(players()) == 6
    }
}

What the toolchain says

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

$ soma check leaderboard.cell
✓ All checks passed.
$ soma test leaderboard.cell
test LeaderboardTests ...
  ✓ assert champion().score == 5100
  ✓ assert ranked()[0].score == 5100
  ✓ assert len(podium()) == 3
  ✓ assert min_by(players(), "score").name == "vex"
  ✓ assert with_ranks()[0].rank == 1
  ✓ assert with_ranks()[5].rank == 6
  ✓ assert len(players()) == 6

7 tests: 7 passed, 0 failed
$ soma verify leaderboard.cell
Verifying leaderboard.cell...

Leaderboard/termination:

  ⚠ handler `with_ranks`: while-loop without provable termination bound (write `while [loop_bound(N)] cond { … }`, or a for-loop over a bounded range)
  ⚠ `run` calls `Leaderboard.with_ranks`, which may not terminate

0 passed, 2 warnings, 0 failures
Temporal: 0 passed, 0 failed
VERIFY OK

Builtins used

len, list, map, max_by, min_by, pluck, print, sort_by, top, with

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