Created
June 21, 2012 13:19
-
-
Save opyate/2965695 to your computer and use it in GitHub Desktop.
Stab at immutable XML stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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