Skip to content

Instantly share code, notes, and snippets.

@dstaley
Last active February 5, 2019 21:19
Show Gist options
  • Save dstaley/ea6f3df2b81d69a6b7d7 to your computer and use it in GitHub Desktop.
Save dstaley/ea6f3df2b81d69a6b7d7 to your computer and use it in GitHub Desktop.
FizzBuzz in Swift using pattern matching
func fizzbuzz(i: Int) -> String {
let result = (i % 3, i % 5)
switch result {
case (0, _):
return "Fizz"
case (_, 0):
return "Buzz"
case (0, 0):
return "FizzBuzz"
default:
return "\(i)"
}
}
for number in 1...100 {
println(fizzbuzz(number))
}
@mattyohe
Copy link

Just as a note... dstaley's answer is incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment