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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:14:30+00:00 2026-05-26T13:14:30+00:00

I am little bit confusing about >> in scala. Daniel said in Scala parser

  • 0

I am little bit confusing about “>>” in scala. Daniel said in Scala parser combinators parsing xml? that it could be used to parameterize the parser base on result from previous parser. Could someone give me some example/hint ? I already read scaladoc but still not understand it.

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-26T13:14:31+00:00Added an answer on May 26, 2026 at 1:14 pm

    As I said, it serves to parameterize a parser, but let’s walk through an example to make it clear.

    Let’s start with a simple parser, that parses a number follow by a word:

    def numberAndWord = number ~ word
    def number        = "\\d+".r
    def word          = "\\w+".r
    

    Under RegexParsers, this will parse stuff like “3 fruits”.

    Now, let’s say you also want a list of what these “n things” are. For example, “3 fruits: banana, apple, orange”. Let’s try to parse that to see how it goes.

    First, how do I parse “N” things? As it happen, there’s a repN method:

    def threeThings = repN(3, word)
    

    That will parse “banana apple orange”, but not “banana, apple, orange”. I need a separator. There’s repsep that provides that, but that won’t let me specify how many repetitions I want. So, let’s provide the separator ourselves:

    def threeThings = word ~ repN(2, "," ~> word)
    

    Ok, that words. We can write the whole example now, for three things, like this:

    def listOfThings = "3" ~ word ~ ":" ~ threeThings
    def word         = "\\w+".r
    def threeThings  = word ~ repN(2, "," ~> word)
    

    That kind of works, except that I’m fixing “N” in 3. I want to let the user specify how many. And that’s where >>, also known as into (and, yes, it is flatMap for Parser), comes into. First, let’s change threeThings:

    def things(n: Int) = n match {
      case 1          => word ^^ (List(_))
      case x if x > 1 => word ~ repN(x - 1, "," ~> word) ^^ { case w ~ l => w :: l }
      case x          => err("Invalid repetitions: "+x)
    }
    

    This is slightly more complicated than you might have expected, because I’m forcing it to return Parser[List[String]]. But how do I pass a parameter to things? I mean, this won’t work:

    def listOfThings = number ~ word ~ ":" ~ things(/* what do I put here?*/)
    

    But we can rewrite that like this:

    def listOfThings = (number ~ word <~ ":") >> {
      case n ~ what => things(n.toInt)
    }
    

    That is almost good enough, except that I now lost n and what: it only returns “List(banana, apple, orange)”, not how many there ought to be, and what they are. I can do that like this:

    def listOfThings   = (number ~ word <~ ":") >> {
      case n ~ what => things(n.toInt) ^^ { list => new ~(n.toInt, new ~(what, list)) }
    }
    def number         = "\\d+".r
    def word           = "\\w+".r
    def things(n: Int) = n match {
      case 1          => word ^^ (List(_))
      case x if x > 1 => word ~ repN(x - 1, "," ~> word) ^^ { case w ~ l => w :: l }
      case x          => err("Invalid repetitions: "+x)
    }
    

    Just a final comment. You might have wondered asked yourself “what do you mean flatMap? Isn’t that a monad/for-comprehension thingy?” Why, yes, and yes! 🙂 Here’s another way of writing listOfThings:

    def listOfThings   = for {
      nOfWhat  <- number ~ word <~ ":"
      n ~ what = nOfWhat
      list     <- things(n.toInt)
    }  yield new ~(n.toInt, new ~(what, list))
    

    I’m not doing n ~ what <- number ~ word <~ ":" because that uses filter or withFilter in Scala, which is not implemented by Parsers. But here’s even another way of writing it, that doesn’t have the exact same semantics, but produce the same results:

    def listOfThings   = for {
      n    <- number
      what <- word
      _    <- ":" : Parser[String]
      list <- things(n.toInt)
    }  yield new ~(n.toInt, new ~(what, list))
    

    This might even give one to think that maybe the claim that “monads are everywhere” might have something to it. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A little bit confusion about some data structure in Python. Could any expert give
Ok, that title is going to be a little bit confusing. Let me try
the title might be a little bit confusing, let me explain, ;) I have
Well, the Endianness theme was always a little bit confusing to me, but i
I've read a little bit about unit testing and was wondering how YOU unit
I'm having a little bit of trouble making a sticky form that will remember
I have a table with about 100.000 rows that used to look more or
I have read some web articles about Three-Tier Architecture. However, I am little bit
I believe that this topic is very important to discuss a little bit since
I have a little bit of a confusing situation :) I have a JavaScript

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.