Skip to content

Instantly share code, notes, and snippets.

@opyate
Created June 21, 2012 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opyate/2965695 to your computer and use it in GitHub Desktop.
Save opyate/2965695 to your computer and use it in GitHub Desktop.
Stab at immutable XML stuff
// see http://stackoverflow.com/questions/11130684/modify-xml-in-scala-without-mutation
import scala.xml._
import scala.xml.transform._
object BlankReplacer extends BasicTransformer {
override def transform(n: Node): NodeSeq = n match {
case <blank/> => {
<input name={ "blank.%d" }/>
}
case elem: Elem => elem.copy(child=elem.child.flatMap(transform _))
case _ => n
}
}
val q = <text>The capitals of Bolivia are <blank/> and <blank/>.</text>
val qt = BlankReplacer(q).toString
lazy val n: Stream[Int] = Stream.cons(1, n.map(_ + 1))
// for any qt
qt.format(n.take(qt.toString.count(_ == '%')).toArray:_*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment