soma.cell

Vowel / consonant counter

Walks each character, classifying letters as vowels or consonants and ignoring everything else. Demonstrates membership testing with contains() over a vowel set and returning a small summary Map. Run: soma run vowel_count.cell --signal analyze "Hello World"

A Soma program from the text part of the verified corpus: 55 lines, cells VowelCount, VowelCountTest. It passes soma check with soma 3.2.4.

How it is built

The cell VowelCount declares 2 signals in its face: count_vowels(s: String) -> Int and analyze(s: String) -> Map. Handlers: count_vowels (line 17) and analyze (line 28).

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

Generated from soma describe on this file.

The source

vowel_count.cell

// ============================================================================
// Vowel / consonant counter
// ============================================================================
//
// Walks each character, classifying letters as vowels or consonants and
// ignoring everything else. Demonstrates membership testing with contains()
// over a vowel set and returning a small summary Map.
//
// Run: soma run vowel_count.cell --signal analyze "Hello World"

cell VowelCount {
    face {
        signal count_vowels(s: String) -> Int
        signal analyze(s: String) -> Map
    }

    on count_vowels(s: String) {
        let low = lowercase(s)
        let n = 0
        for i in range(0, len(low)) {
            if contains("aeiou", low[i]) {
                n = n + 1
            }
        }
        return n
    }

    on analyze(s: String) {
        let low = lowercase(s)
        let vowels = 0
        let consonants = 0
        for i in range(0, len(low)) {
            let c = low[i]
            if c >= "a" && c <= "z" {
                if contains("aeiou", c) {
                    vowels = vowels + 1
                } else {
                    consonants = consonants + 1
                }
            }
        }
        return map("vowels", vowels, "consonants", consonants)
    }
}

cell test VowelCountTest {
    rules {
        assert count_vowels("hello") == 2
        assert count_vowels("xyz") == 0
        assert count_vowels("AEIOU") == 5
        assert analyze("Hello World")["vowels"] == 3
        assert analyze("Hello World")["consonants"] == 7
    }
}

What the toolchain says

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

$ soma check vowel_count.cell
✓ All checks passed.
$ soma test vowel_count.cell
test VowelCountTest ...
  ✓ assert count_vowels("hello") == 2
  ✓ assert count_vowels("xyz") == 0
  ✓ assert count_vowels("AEIOU") == 5
  ✓ assert analyze("Hello World")["vowels"] == 3
  ✓ assert analyze("Hello World")["consonants"] == 7

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

contains, len, lowercase, map, range

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