Skip to content

Instantly share code, notes, and snippets.

@retronym
Created June 13, 2011 20:39
Show Gist options
  • Save retronym/1023634 to your computer and use it in GitHub Desktop.
Save retronym/1023634 to your computer and use it in GitHub Desktop.
attribute-extractor.scala
scala> import xml._
import xml._
scala> class Attr(name: String) { def unapply(e: Elem) = e.attribute(name).map(_.text) }
defined class Attr
scala> val IdAttr = new Attr("id")
IdAttr: Attr = Attr@6b2dc21d
scala> <a id="123"/> match { case IdAttr("123") => "!" }res3: java.lang.String = !
scala> <a id="123"/> match { case IdAttr(id) => id }
res4: String = 123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment