Giter VIP home page Giter VIP logo

Comments (6)

essandess avatar essandess commented on July 23, 2024

I added this code to address the issue, 6fec1a3:

       (<++>) a b = (++) <$> a <*> b
         (<:>) a b = (:) <$> a <*> b
         number = many1 digit
         subnumber = char '.' <:> number
         versionnumber = number <|> number <++> subnumber
         versionParser = (\x -> info{_version = read x}) <$> (string "Version: " *> versionnumber)

Ideally, this should be the parser many1 digit `sepBy` char '.' to get things like version 8.4.3, but this gives the type error, which I haven't followed through with yet:

    • Couldn't match type ‘[Char]’ with ‘Char’
      Expected type: Text.Parsec.Prim.ParsecT s u m String
        Actual type: Text.Parsec.Prim.ParsecT s u m [[Char]]
    • In the second argument of ‘(<$>)’, namely
        ‘(string "Version: " *> (many1 digit `sepBy` char '.'))’
      In the expression:
        (\ x -> info {_version = read x})
          <$> (string "Version: " *> (many1 digit `sepBy` char '.'))
      In an equation for ‘versionParser’:
          versionParser
            = (\ x -> info {_version = read x})
                <$> (string "Version: " *> (many1 digit `sepBy` char '.'))
   |
93 |         versionParser = (\x -> info{_version = read x}) <$> (string "Version: " *> (many1 digit `sepBy` char '.'))
   |                                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

from adblock2privoxy.

essandess avatar essandess commented on July 23, 2024

@qrilka If you have a moment, I have a educational Haskell question I’ve been unable to tackle.

We would like to parse version numbers like 8.4.3.

The original code’s parser used many1 digit, and would grab the 8, but not the .4.3.

I thought that the obvious fix would be to replace many1 digit With many1 digit `sepBy` char ‘.’. But this fails to compile on a mismatched type of String versus [[Char]]. So I hacked in the code block above that would parse 8.4, and omit the .3.

Why doesn’t the sepBy work here?

from adblock2privoxy.

qrilka avatar qrilka commented on July 23, 2024

@essandess I'm not sure I understand your point about "not work" here - sepBy works just as it's supposed to work - it constructs a list of values in the end with separator excluded, i.e. for "8.4.3" you'll get ["8","4","3"]. I see _version is an Integer and I wonder how could you store a multicomponent version there, something line [Int] would be more sensible if number of version components is not fixed (though I don't yet know how you use that information)

from adblock2privoxy.

essandess avatar essandess commented on July 23, 2024

Thanks again for the Haskell n00b pointers @qrilka!

What I mean is that the code fragment many1 digit `sepBy` char ‘.’ does not compile in this statement:

-- versionnumber = many1 digit -- this compiles!
versionnumber = many1 digit `sepBy` char ‘.’  -- this doesn't compile!!! 
-- versionnumber = (++) <$> many1 digit `sepBy` char ‘.’  -- this doesn't compile either!!! 
versionParser = (\x -> info{_version = read x}) <$> (string "Version: " *> versionnumber)

What's the correct sepBy (or equivalent) parser that will grab the string "8.4.3" from a line that looks like:

Version: 8.4.3

Just to keep it simple, I'd like to parse the string alone, and ignore that fact that it is comprised of things that could be cast as Int type.

In Python, this would be something like '.'.join(["8","4","3"]), after the parser found the "Version: 8.4.3" and sepBy converted it to ["8","4","3"].

from adblock2privoxy.

essandess avatar essandess commented on July 23, 2024

@qrilka Thanks again for the pointer. I got it:

intercalate "." <$> many1 digit `sepBy` char '.'

from adblock2privoxy.

qrilka avatar qrilka commented on July 23, 2024

I didn't write anything here today :)
Theoretically you could do that in the parser already though it looks not quite pleasant:

λ> parse (do{d1 <- many1 digit; dotDs <- many1 $ (:) <$> char '.' <*> many1 digit; return $ concat (d1:dotDs)}) "" "18.4.3"
Right "18.4.3"

from adblock2privoxy.

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.