def interpolate(text: String, vars: Map[String, String]) =
"""\$\{([^}]+)\}""".r.replaceAllIn(text, (_: scala.util.matching.Regex.Match) match {
case Regex.Groups(v) => vars.getOrElse(v, "")
})
Which can then be used like this:
scala> "The ${name} of the ${game}"
res0: java.lang.String = The ${name} of the ${game}
scala> Map("name" -> "X", "game" -> "Y")
res1: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(name -> X, game -> Y)
scala> interpolate(res0, res1)
res2: String = The X of the Y
What has arrived that makes that possible/easier?
ReplyDeleteI guess it's this commit: http://lampsvn.epfl.ch/trac/scala/changeset/20661
ReplyDeleteWhich could, with an implicit, written as
ReplyDelete"The ${name} of the ${game}".fill("name" -> "X", "game" -> "Y")
That is a devious idea... :-)
ReplyDeleteor use expressions in {} in xml and convert the xml to string with the text method of xml
ReplyDeleteIt's not as convenient, but you might use
ReplyDelete"The %s of the %s".format("X", "Y")
using the same thread comments (Sorry, cant find better place ) to another question (easy mode) . If i desires , for exemplo, increment all elements from a List, what is the best approach ?
ReplyDeleteI can´t use the foreach, allright ? Cause lists are immutable, right ?
list foreach ( e => e+1 ) do not increment each element by one.
What should i do ?
Bruno, o melhor lugar para fazer tal pergunta é http://www.stackoverflow.com, em minha opinião.
ReplyDeleteEm resposta à sua pergunta, "list map (e => e + 1)" retorna uma nova lista com todos os elementos incrementados em 1.
$ scala-2.8.0.r21356-b20100407020120/bin/scalac Foo.scala
ReplyDeleteFoo.scala:20: error: missing parameter type for expanded function ((x0$1) => x0$1 match {
case Regex.Groups((v @ _)) => vars.getOrElse(v, "")
})
"""\$\{([^}]+)\}""".r.replaceAllIn(text, {
^
one error found
This comment has been removed by a blog administrator.
ReplyDeleteMost curious. It used to work, and it _ought_ to work, but it obviously isn't. I fixed by making the match explicit.
ReplyDeleteYou don't have to specify the type explicitly again. This is sufficient:
ReplyDeletedef interpolate(text: String, vars: Map[String, String]) =
"""\$\{([^}]+)\}""".r.replaceAllIn(text, _ match {
case Regex.Groups(v) => vars.getOrElse(v, "")
})
And IMHO the "_ match" makes indeed sense as opposed to the original version.