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 7638945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:17:50+00:00 2026-05-31T08:17:50+00:00

I wrote some parser from combinatory library. I want a generic function that transform

  • 0

I wrote some parser from combinatory library. I want a generic function that transform any size of nest ~ into a list. How to do this ?

Here is my example of parser I use (my real parser has a very long chain ~ so I want to avoid my current solution which is in comment below).

object CombinatorParser extends RegexParsers {

  lazy val a = "a"
  lazy val b = "b"
  lazy val c = "c"
  lazy val content = a ~ b ~ c // ^^ {case a~b => a::b::c::Nil work but I want something more general that work for any ~ length.
}

object CombinatorTesting {

  def main(args:Array[String]) {
    val testChar = "abc"
    val output = CombinatorParser.parseAll(CombinatorParser.content, testChar)
    println(output) // ((a~b)~c) but I want List(a,b,c)
  }
}
  • 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-31T08:17:51+00:00Added an answer on May 31, 2026 at 8:17 am

    This is a good (and fairly simple) application for the kind of generic programming techniques exemplified in shapeless.

    Given your definition,

    object CombinatorParser extends RegexParsers {
      lazy val a = "a"
      lazy val b = "b"
      lazy val c = "c"
      lazy val content = a ~ b ~ c
    }
    

    We can recursively define a type class that will flatten it’s results as follows,

    import CombinatorParser._
    

    First we define a trait which (abstractly) flattens an arbitrary match M to a List[String],

    trait Flatten[M] extends (M => List[String]) {
      def apply(m : M) : List[String]
    }
    

    Then we provide type class instances for all the shapes of M that we’re interested in: in this case, String, A ~ B and ParseResult[T] (where A, B and T are all types for which there are Flatten instances),

    // Flatten instance for String
    implicit def flattenString = new Flatten[String] {
      def apply(m : String) = List(m) 
    }
    
    // Flatten instance for `A ~ B`. Requires Flatten instances for `A` and `B`. 
    implicit def flattenPattern[A, B]
      (implicit flattenA : Flatten[A], flattenB : Flatten[B]) =
        new Flatten[A ~ B] {
          def apply(m : A ~ B) = m match {
            case a ~ b => flattenA(a) ::: flattenB(b)
          } 
    }
    
    // Flatten instance for ParseResult[T]. Requires a Flatten instance for T.
    implicit def flattenParseResult[T]
      (implicit flattenT : Flatten[T]) = new Flatten[ParseResult[T]] {
        def apply(p : ParseResult[T]) = (p map flattenT) getOrElse Nil 
    }
    

    Finally we can define a convenience function to simplify applying Flatten instances to parse results,

    def flatten[P](p : P)(implicit flatten : Flatten[P]) = flatten(p)
    

    And now we’re ready to go,

    val testChar = "abc"
    val output = parseAll(content, testChar)
    println(output)          // ((a~b)~c) but I want List(a, b, c)
    
    val flattenedOutput = flatten(output)
    println(flattenedOutput) // List(a, b, c)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I wrote some perl that would parse results returned from the Amazon Web
I wrote some wcf interface ( REST ) that need to be called from
I wrote some code with a lot of recursion, that takes quite a bit
I wrote some naive code(in the sense that it's synchronous calls) for a tableview
I recently wrote some javascript code that filled a drop down list based on
I wrote a simple Tkinter based Python application that reads text from a serial
For my project I wrote a small config class that loads its data from
I've grabbed some Scala CSV parsing code from here: Use Scala parser combinator to
I'm trying to write an xml parser but seem to be running into some
I wrote simple NMEA parser and I'm reading latitude and longitude from GPS. Values

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.