Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

As the title suggests, this is my FizzBuzz implementation in Scala.

I started learning Scala (and functional programming in general) in small steps about two months ago, and one recent assignment was the FizzBuzz task. Here is the simple code I came up with to solve the problem. It runs perfectly, but I figured CodeReview would be able to easily point out thinks I'm missing.

I do think that the hardcoded magic values (3,5,15,"Fizz","Buzz","FizzBuzz") are slightly problematic, but I'm not sure if that's general consensus or what the best way to handle that is.

###FizzBuzz.scala package fizzbuzz

FizzBuzz.scala

package fizzbuzz
object FizzBuzz {
 def fizzbuzz(i: Int): String = i match {
 case i if i % 15 == 0 => "FizzBuzz" //because i | 15 is the same as i | 5 and i | 3
 case i if i % 5 == 0 => "Buzz"
 case i if i % 3 == 0 => "Fizz"
 case i => i toString
 }
 def fizzbuzz(start: Int, end: Int): List[String] = (start to end map fizzbuzz) toList
 //assignment requirement: return list of first 100 fizzbuzzed numbers
 def fizzbuzz100(): List[String] = fizzbuzz(1,100)
}

###Main.scala (sample usage of FizzBuzz object) package fizzbuzz

Main.scala (sample usage of FizzBuzz object)

package fizzbuzz
object main {
 def main(args: Array[String]): Unit = {
 FizzBuzz.fizzbuzz100() map println
 }
}

As the title suggests, this is my FizzBuzz implementation in Scala.

I started learning Scala (and functional programming in general) in small steps about two months ago, and one recent assignment was the FizzBuzz task. Here is the simple code I came up with to solve the problem. It runs perfectly, but I figured CodeReview would be able to easily point out thinks I'm missing.

I do think that the hardcoded magic values (3,5,15,"Fizz","Buzz","FizzBuzz") are slightly problematic, but I'm not sure if that's general consensus or what the best way to handle that is.

###FizzBuzz.scala package fizzbuzz

object FizzBuzz {
 def fizzbuzz(i: Int): String = i match {
 case i if i % 15 == 0 => "FizzBuzz" //because i | 15 is the same as i | 5 and i | 3
 case i if i % 5 == 0 => "Buzz"
 case i if i % 3 == 0 => "Fizz"
 case i => i toString
 }
 def fizzbuzz(start: Int, end: Int): List[String] = (start to end map fizzbuzz) toList
 //assignment requirement: return list of first 100 fizzbuzzed numbers
 def fizzbuzz100(): List[String] = fizzbuzz(1,100)
}

###Main.scala (sample usage of FizzBuzz object) package fizzbuzz

object main {
 def main(args: Array[String]): Unit = {
 FizzBuzz.fizzbuzz100() map println
 }
}

As the title suggests, this is my FizzBuzz implementation in Scala.

I started learning Scala (and functional programming in general) in small steps about two months ago, and one recent assignment was the FizzBuzz task. Here is the simple code I came up with to solve the problem. It runs perfectly, but I figured CodeReview would be able to easily point out thinks I'm missing.

I do think that the hardcoded magic values (3,5,15,"Fizz","Buzz","FizzBuzz") are slightly problematic, but I'm not sure if that's general consensus or what the best way to handle that is.

FizzBuzz.scala

package fizzbuzz
object FizzBuzz {
 def fizzbuzz(i: Int): String = i match {
 case i if i % 15 == 0 => "FizzBuzz" //because i | 15 is the same as i | 5 and i | 3
 case i if i % 5 == 0 => "Buzz"
 case i if i % 3 == 0 => "Fizz"
 case i => i toString
 }
 def fizzbuzz(start: Int, end: Int): List[String] = (start to end map fizzbuzz) toList
 //assignment requirement: return list of first 100 fizzbuzzed numbers
 def fizzbuzz100(): List[String] = fizzbuzz(1,100)
}

Main.scala (sample usage of FizzBuzz object)

package fizzbuzz
object main {
 def main(args: Array[String]): Unit = {
 FizzBuzz.fizzbuzz100() map println
 }
}
Source Link

FizzBuzz in Scala

As the title suggests, this is my FizzBuzz implementation in Scala.

I started learning Scala (and functional programming in general) in small steps about two months ago, and one recent assignment was the FizzBuzz task. Here is the simple code I came up with to solve the problem. It runs perfectly, but I figured CodeReview would be able to easily point out thinks I'm missing.

I do think that the hardcoded magic values (3,5,15,"Fizz","Buzz","FizzBuzz") are slightly problematic, but I'm not sure if that's general consensus or what the best way to handle that is.

###FizzBuzz.scala package fizzbuzz

object FizzBuzz {
 def fizzbuzz(i: Int): String = i match {
 case i if i % 15 == 0 => "FizzBuzz" //because i | 15 is the same as i | 5 and i | 3
 case i if i % 5 == 0 => "Buzz"
 case i if i % 3 == 0 => "Fizz"
 case i => i toString
 }
 def fizzbuzz(start: Int, end: Int): List[String] = (start to end map fizzbuzz) toList
 //assignment requirement: return list of first 100 fizzbuzzed numbers
 def fizzbuzz100(): List[String] = fizzbuzz(1,100)
}

###Main.scala (sample usage of FizzBuzz object) package fizzbuzz

object main {
 def main(args: Array[String]): Unit = {
 FizzBuzz.fizzbuzz100() map println
 }
}
lang-scala

AltStyle によって変換されたページ (->オリジナル) /