src/main/scala/roman_numerals.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
object RomanNumeral {
  def apply(number: Int) = new RomanNumeral(number)

  val Numerals = List(
    1000 -> "M",
    900  -> "CM",
    500  -> "D",
    400  -> "CD",
    100  -> "C",
    90   -> "XC",
    50   -> "L",
    40   -> "XL",
    10   -> "X",
    9    -> "IX",
    5    -> "V",
    4    -> "IV",
    1    -> "I"
  )
}

class RomanNumeral(val number: Int) {
  def unfoldRight[A, B](seed: B)(f: B => Option[(A, B)]): List[A] = f(seed) match {
    case Some((a, b)) => a :: unfoldRight(b)(f)
    case None => Nil
  }

  lazy val value = unfoldRight(number) {
    // Each time, take the largest value <= n and subtract it
    n => RomanNumeral.Numerals.find { _._1 <= n } map {
      case (value, numerals) => (numerals, n - value)
    }
  }.mkString
}

@abo64 thinks this looks great

Comments


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