soma.cell

FizzBuzz

The classic interview problem, implemented in Soma.

Example 017 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.

How it is built

The cell FizzBuzz has no face and no memory. Handlers: run (line 26).

Generated from soma describe on this file.

The source

017_fizzbuzz.cell

// ============================================================================
// 017 — FizzBuzz
// ============================================================================
// The classic interview problem, implemented in Soma.
//
// Run:   soma run 017_fizzbuzz.cell run
// Expect:
//   1
//   2
//   Fizz
//   4
//   Buzz
//   Fizz
//   7
//   8
//   Fizz
//   Buzz
//   11
//   Fizz
//   13
//   14
//   FizzBuzz

cell FizzBuzz {

    on run() {
        let i = 1
        while i <= 15 {
            // Check divisibility using modulo (%)
            let by3 = i % 3
            let by5 = i % 5

            if by3 == 0 {
                if by5 == 0 {
                    // Divisible by both 3 and 5
                    print("FizzBuzz")
                } else {
                    // Divisible by 3 only
                    print("Fizz")
                }
            } else {
                if by5 == 0 {
                    // Divisible by 5 only
                    print("Buzz")
                } else {
                    // Not divisible by either
                    print("{i}")
                }
            }

            i = i + 1
        }

        return "done"
    }
}

What the toolchain says

$ soma check 017_fizzbuzz.cell
✓ All checks passed.
$ soma run 017_fizzbuzz.cell run
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
done