Code Monkey home page Code Monkey logo

Comments (6)

johnynek avatar johnynek commented on May 28, 2024 2

You might want to take a look at:

https://github.com/typelevel/cats-parse/blob/main/core/shared/src/main/scala/cats/parse/Numbers.scala

Also note the repAs method that can build the string in a more efficient manner than you do here.

Lastly, you can parse a signed int32 without parsing the string and maybe failing, but it is more work: you either parse a - or not. If you don't see a - you either see 0, or 1 followed by 0 to 8 digits, etc...

This is like building a regular expression for a limited integer. It's probably worth implementing and adding to Numbers so people don't have to struggle with it.

That said, I do question if a parse error is what you want here vs an error message that says the number is too large. That can be done by using Numbers + filter (or flatMap but filter can be more efficient).

from cats-parse.

johnynek avatar johnynek commented on May 28, 2024 1

Suppose you have another parser you'd like to embed inside a cats parse parser, that would be one example.

A second example is supporting something we could support but don't (e.g. getting access to the whole String).

I don't think it is a great idea, but it is an escape hatch if someone gets in an unusual place.

from cats-parse.

elimirks avatar elimirks commented on May 28, 2024

Do you have examples of when this is necessary?

from cats-parse.

morgen-peschke avatar morgen-peschke commented on May 28, 2024

I've got a concrete example from something I'm working on at the moment.

Unless I'm missing a standard combinator, this is currently how to parse an integer:

final val integer: Parser[Int] =
  Parser
    .charIn('0' to '9')
    .rep
    .map { chars =>
      chars
        .foldLeft(new StringBuilder(chars.length))(_ append _)
        .mkString
    }
    .flatMap { input =>
      Either
        .catchNonFatal(input.toInt)
        .fold(
          _ => Parser.failWith(s"Invalid number: $input"),
          Parser.pure
        )
    }

from cats-parse.

elimirks avatar elimirks commented on May 28, 2024

I do this for unsigned ints:

  def safeToInt(value: String): Option[Int] =
    Try(value.toInt).toOption

  val uintP: Parser[Int] = Numbers.digits.mapFilter(safeToInt)

from cats-parse.

morgen-peschke avatar morgen-peschke commented on May 28, 2024

Thanks for pointing me towards Numbers. Aside from the direct stuff, it's got a lot of good examples.

In my case, a parse error is desirable because I'm using it to parse command line arguments, so an out of bounds number is going to be less common than a parse error caused by an extraneous quote.

from cats-parse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.