Skip to content

Instantly share code, notes, and snippets.

@markus1189
Created May 20, 2013 08:19
Show Gist options
  • Save markus1189/5611013 to your computer and use it in GitHub Desktop.
Save markus1189/5611013 to your computer and use it in GitHub Desktop.
import scalaz.std.stream._
import scalaz.std.tuple._
import scalaz.syntax.bifunctor._
def romanize(number: Int) = {
val numerals = Seq( ( "M", 1000), ("CM", 900)
, ( "D", 500) , ("CD", 400)
, ( "C", 100) , ("XC", 90)
, ( "L", 50) , ("XL", 40)
, ( "X", 10) , ("IX", 9)
, ( "V", 5) , ("IV", 4)
, ( "I", 1)
)
def next(in: Int) = numerals find(_._2 <= in) map (_.rightMap(in - _))
unfold(number)(next).mkString("")
}
assert(romanize(422) == "CDXXII")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment