Apologies if this is a FAQ, I did not find it anywhere. It is probably a newbie Scala and/or functional programming question. I have lots of Java and OO experience, but I am new to Scala and FP.
Lets say I have a list that perhaps has:
a, b, b, c1, b, d, c2, d, a, ce, b, a, c1, b, ce, a, b
Now I’m going to process this list and return an XML tree (scala.xml.NodeSeq or whatever). The tricky part is I need to replace any cases of c<n> with a span that encloses any following items, up to the next ce. Further complicating things is nesting must be handled, but any “ce” items encountered need to close out all pending tags.
So I want to get something like this out:
<a/>
<b/>
<b/>
<span style="style1">
<b/>
<d/>
<span style="style2">
<d/>
<a/>
</span>
</span>
<b/>
<a/>
<span style="style1">
<b/>
</span>
<a/>
<b/>
This is in Scala, and I would prefer to do this in a “purely functional way”, and using Scala best practices. I’m quite frustrated, I cannot get my head around this.
Thanks.
Some recursion for the win: