tolgap
tolgap

Reputation: 9778

Take a single succeeding value out of a list of optional Parsers

I have a function that has a signature

tryParseAllFilesInDir :: FilePath -> [Parsec ByteString () (Maybe a)]

It tries to parse all files in a directory with a specific Parser. It is a given that only one file will actually succeed, but I don't know which file at runtime.

I want to take the list of optional Parsers, and take the only succeeding Parser value out. I don't know what functions I would use to achieve this.

I somehow have to go from [Parsec ByteString () (Maybe a)] -> [Maybe a], and then [Maybe a] -> Just a.

If there is a better approach to doing this, I would appreciate those as well.

Upvotes: 4

Views: 75

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 152867

You can run the parsers with runParser. This will give you a list of type [Either ParseError (Maybe a)]; you can use partitionEithers to extract just the successful parses.

Upvotes: 5

Related Questions