src/main/scala/raindrops.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import scala.collection.SortedMap

object Raindrops {
  def apply() = this

  val Noises = SortedMap(3 -> "Pling", 5 -> "Plang", 7 -> "Plong")

  private def noises(n: Int) = Noises.foldLeft("") {
    case (acc, (factor, noise)) => if (n % factor == 0) acc + noise else acc
  }

  def convert(n: Int) = {
    val values = noises(n)
    if (values.nonEmpty) values else n.toString
  }
}

@abo64 thinks this looks great

Comments

Removed mkString but decided to stick with the existing approach to convert

helenst commented 19 May 2016 at 17:39 UTC

You're not logged in right now. Please login via GitHub to comment