Michael
Michael

Reputation: 42050

Function to transform XML instead of RuleTransformer

This is a follow-up to my previous question.

I have written a simple function, which transforms an XML much faster than RuleTransformer

def trans(node: Node, pf: PartialFunction[Node, Node]): Node = {
  val n1 = if (pf.isDefinedAt(node)) pf(node) else node
  n1 match {
    case e: Elem => e.copy(child = e.child map (c => trans(c, pf)))
    case other => other
  }
}

Does it make sense ? How would you correct/improve that code ?

Upvotes: 0

Views: 94

Answers (1)

millhouse
millhouse

Reputation: 10007

I seem to be allergic to if statements these days, so if it was my code, the only adjustment would be using PartialFunction's applyOrElse to eliminate that:

def trans2(node: Node, pf: PartialFunction[Node, Node]): Node = {
  val n1 = pf.applyOrElse(node, identity[Node])
  n1 match {
    case e: Elem => e.copy(child = e.child map (c => trans2(c, pf)))
    case other => other
  }
}

I'm the first to admit that the "obviousness" may well have been reduced :-(

Upvotes: 1

Related Questions