Attoparsec parser and maybe type

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Attoparsec parser and maybe type

PICCA Frederic-Emmanuel
Hello, I have a type  compose of 5 doubles

data MyData = MyData Double Double Double Double Double

now I have a file to parse whcih contain this

| 1.0 2.0 3.0 4.0 5.0 | or | ---------no result ------------- |

So I would like to create  a parser which return a Maybe Mydata
| 1.0 2.0 3.0 4.0 5.0 | -> Just MyData
| ---------no result ------------- | -> Nothing

How should I proceed

I can write really trivially both parser but I do not know how to combine all this

MyDataP1 = MyData <$> scientific <*> scientific
NothingP = string "--------------- no result ----------------"

Thanks for your help

Frederic

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Attoparsec parser and maybe type

Francesco Ariis
Hello Frederic,

On Wed, Aug 29, 2018 at 01:01:42PM +0000, PICCA Frederic-Emmanuel wrote:
> Hello, I have a type  compose of 5 doubles
>
> data MyData = MyData Double Double Double Double Double
>
> now I have a file to parse whcih contain this
>
> | 1.0 2.0 3.0 4.0 5.0 | or | ---------no result ------------- |

<|> (or `choice`) is the way to do it [1]. Let us know if that worked!

[1] http://hackage.haskell.org/package/attoparsec-0.13.2.2/docs/Data-Attoparsec-Combinator.html#v:choice
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Attoparsec parser and maybe type

PICCA Frederic-Emmanuel
powderWilsonP :: Parser (Maybe PowderWilson)
powderWilsonP = powderWilsonP1 <|> powderWilsonP2
    where
      powderWilsonP1 :: Parser (Maybe PowderWilson)
      powderWilsonP1 = Just <$> ( PowderWilson
                                  <$> doubleP
                                  <*> doubleP
                                  <*> doubleP
                                  <*> doubleP
                                  <*> doubleP
                                )

      powderWilsonP2 :: Parser (Maybe PowderWilson)
      powderWilsonP2 = do
        skipSpace
        _ <- string "---------no results -----------"
        pure Nothing

I will test it :))

thanks
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners