soma.cell

Title case — capitalize the first letter of every word

Splits on spaces, uppercases each word's first character and lowercases the rest, then re-joins. Demonstrates substring slicing and per-word transforms mapped over the word list. Run: soma run title_case.cell --signal titlecase "the QUICK brown fox"

A Soma program from the text part of the verified corpus: 40 lines, cells TitleCase, TitleCaseTest. It passes soma check with soma 3.2.4.

How it is built

The cell TitleCase declares 2 signals in its face: capitalize(word: String) -> String and titlecase(text: String) -> String. Handlers: capitalize (line 17) and titlecase (line 24).

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

Generated from soma describe on this file.

The source

title_case.cell

// ============================================================================
// Title case — capitalize the first letter of every word
// ============================================================================
//
// Splits on spaces, uppercases each word's first character and lowercases the
// rest, then re-joins. Demonstrates substring slicing and per-word transforms
// mapped over the word list.
//
// Run: soma run title_case.cell --signal titlecase "the QUICK brown fox"

cell TitleCase {
    face {
        signal capitalize(word: String) -> String
        signal titlecase(text: String) -> String
    }

    on capitalize(word: String) {
        if len(word) == 0 { return "" }
        let head = uppercase(substring(word, 0, 1))
        let tail = lowercase(substring(word, 1, len(word)))
        return head + tail
    }

    on titlecase(text: String) {
        let words = split(text, " ")
        let out = words |> map(w => capitalize(w))
        return join(out, " ")
    }
}

cell test TitleCaseTest {
    rules {
        assert capitalize("hello") == "Hello"
        assert capitalize("WORLD") == "World"
        assert capitalize("a") == "A"
        assert titlecase("the quick brown fox") == "The Quick Brown Fox"
        assert titlecase("MIXED case INPUT") == "Mixed Case Input"
    }
}

What the toolchain says

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

$ soma check title_case.cell
✓ All checks passed.
$ soma test title_case.cell
test TitleCaseTest ...
  ✓ assert capitalize("hello") == "Hello"
  ✓ assert capitalize("WORLD") == "World"
  ✓ assert capitalize("a") == "A"
  ✓ assert titlecase("the quick brown fox") == "The Quick Brown Fox"
  ✓ assert titlecase("MIXED case INPUT") == "Mixed Case Input"

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

join, len, lowercase, map, split, substring, uppercase

Source file: /corpus/text/title_case.cell · examples/corpus/text/title_case.cell