March kata – FizzBuzz

The March kata was FizzBuzz. This is my third kata done in Scala. For the full source code, go to GitHub.

class FizzyBuzzyInt(i: Int) {
  def isFizzy = divisibleBy(3) || contains(3)
  def isNotFizzy = !isFizzy
  def isBuzzy = divisibleBy(5) || contains(5)
  def isNotBuzzy = !isBuzzy

  private def divisibleBy(divisor: Int) = i % divisor == 0
  private def contains(n: Int) = i.toString contains n.toString

class FizzBuzz {
  implicit def extendInt(n: Int) = new FizzyBuzzyInt(n)

  def printSequence(out: PrintStream) = {
    (1 to 100).foreach(n => out.println(sayOnNumber(n)))

  private def sayOnNumber(number: Int): String = {
    number match {
      case n if (n isFizzy) && (n isNotBuzzy) => "Fizz"
      case n if (n isBuzzy) && (n isNotFizzy) => "Buzz"
      case n if (n isFizzy) && (n isBuzzy) => "FizzBuzz"
      case _ => number.toString

Like for Roman Numerals I used Scala’s pattern matching. I find it extremely powerful, especially if you have a Java/C++ background. I suppose I’m obeying Maslow’s Law of the instrument (“if all you have is a hammer, everything looks like a nail”). Anyway, this is the purpose of a kata – to play with language capabilities on a safe playground.

To use the hammer ever more, I want to implement fizzy and buzzy predicates using Scala’s case classes and combine them with the pattern matching.

Specs added to maven-scala-quickstart

I’ve recently added Specs (1.6.7 version) to the maven-scala-quickstart project. If you want to use Specs only and remove other testing libraries, keep JUnit dependency. It is required by Specs.

Just a reminder: clone this Git repository and start coding:

January kata – String Calculator

My first kata I ever did was due to 12 meses 12 katas initiative (12 months 12 katas):

Un mes una kata, ¡mejora tu arte y compártelo con los demás! Ese es el objetivo principal de esta iniciativa que nace de una comunidad preocupada por mejorar sus habilidades y así crear software de más calidad.

Every month, one kata. Improve your coding skills and share them with others! This is the main objective of this initiative, that was conceived in a community striving to improve its skills and thus create better software.

The january kata was String Calculator. I implemented it in Scala with Scala Test. My solution is listed below. For the full source code, go to GitHub. I used this kata to start learning Scala. My progress was very slow: I was combining coding with reading (blog and books about Scala and functional programming).

import java.util.regex.Pattern.quote

class StringCalculator {
  private val defaultSeparators = """,|\n"""

  def sum(expression: String): Int = {
    val (separators, summands) = splitIntoSeparatorsAndSummands(expression)

    val numericSummands = (separators.r).split(summands).filter(_ != "").map(_.toInt)
    val negativeSummands = numericSummands.filter(_ < 0)
    if (negativeSummands.nonEmpty) throw new IllegalArgumentException(negativeSummands.mkString(","))
    numericSummands.filter(_ <= 1000) .foldLeft(0)((sum, summand) => {
      sum + summand

  private def splitIntoSeparatorsAndSummands(expression: String) = {
    val separatorsRegex = """//(.+)\n""".r
    (separatorsRegex findFirstMatchIn expression) match {
      case Some(separatorsMatch) => (convertFoundSeparatorsToRegex(, separatorsMatch.after.toString)
      case None => (defaultSeparators, expression)

  private def convertFoundSeparatorsToRegex(separators: String) = {
    val singleSeparatorRegex = """\[.+?\]""".r
    val quotedSeparators = for(sep <- singleSeparatorRegex findAllIn separators)
      yield quote(sep.replaceAll("""[\[\]]""", ""))


Next task: Roman Numerals. This will be tough, because I’m biased: I saw an elegant solution a couple of weeks ago and it was broadly discussed in the Agile Spain group.

Groovy and Scala quickstart with Maven

Learning Groovy and Scala I did some spikes and katas with Maven. If you want to quickstart a project either with Groovy or with Scala, you may find my quickstart projects useful.


For Scala 2.8.1, JUnit 4.8.2, Hamcrest 1.2 and ScalaTest 1.2, clone this Git repository:


For Groovy 1.7.6 and JUnit 4.8.2:

Happy coding!