Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4577454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:20:53+00:00 2026-05-21T20:20:53+00:00

Apologies if this is a FAQ, I did not find it anywhere. It is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:20:54+00:00Added an answer on May 21, 2026 at 8:20 pm

    Some recursion for the win:

      import scala.xml._
      import scala.annotation.tailrec
    
      val list = List("a", "b", "b", "c1", "b", "d", "c2", "d", "a", "ce", "b", "a", "c1", "b", "ce", "a", "b")
    
      case class Processor(value: NodeSeq = Nil, rest: List[String] = Nil, isSub: Boolean = false)
    
      @tailrec
      def toXml(processor: Processor): Processor = 
        processor.rest match {
          case head::tail if(head == "ce") =>   
            if(processor.isSub) Processor(processor.value, processor.rest)        
            else toXml(Processor(processor.value, tail))
    
          case head::tail if(head.startsWith("c")) => 
            val result = toXml(Processor(Nil, tail, true)) 
            toXml(processor.copy(value = processor.value ++ <span>{result.value}</span>, rest = result.rest))  
    
          case head::tail => toXml(processor.copy(value = processor.value ++ Elem(null, head, null, TopScope), rest = tail))
    
          case Nil => processor
        }
    
      def toXml(input: List[String]): NodeSeq = toXml(Processor(Nil, input)).value
    
    
    scala> toXml(list).toString
    res28: String = <a></a><b></b><b></b><span><b></b><d></d><span><d></d><a></a></span></span><b></b><a></a><span><b></b></span><a></a><b></b>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies if this is a duplicate, but I've not managed to find this question
Apologies if this is a repost, I could not find the search terms to
Apologies if this has been asked before (I couldn't find the answer anywhere), but
Apologies if this question has been asked before, I couldn't find anything similar. I
Apologies if this is straight forward, but I have not found any help in
Apologies if this is a stupid question - I'm an Android and Ant newbie.
Apologies if this question is already posted. I didn't find the answer i was
Apologies if this a newbish question, but I'm new to Mac programming, and thought
Apologies for this question but I can't find proper answer on the web. I'm
Apologies if this question has been asked. I couldn't find it, but if it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.