FizzBuzz in Scala
✓ Published0🌍 Public
PP7h
Last edited Feb 4, 2017
Created on Feb 4, 2017
This example demonstrates a functional implementation of FizzBuzz for numbers 1 through 30, using Scala’s pattern matching with custom extractors. The code defines a `MultipleOf` case class whose `unapply` method returns `Some(x/n)` when the input is divisible by `n`, enabling concise matching against `multipleOfThree`, `multipleOfFive`, and `multipleOfFifteen`. Each number is mapped through a `fizzbuzz` function that returns the appropriate string or the original integer, then printed sequentially via `map println`. The example highlights Scala’s case classes, partial functions, and the map-collect-print idiom.
AI-generated description