-
Notifications
You must be signed in to change notification settings - Fork 51
Implement Compactable
and Filterable
for ParserT
?
#84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Tried for a bit to implement Insight appreciated |
Looked a bit more into it and i've now written a If i were to implement a |
Hi @3ddyy! Thanks for opening the issue and providing a good description of how your I'm a maintainer of this library, but I'm unfamiliar with That said, as we're a bit thinly-spread across a number of libraries it's usually best to open the PR (if it isn't too much additional effort) so that the other maintainers have something more concrete to look at and there is less action to take to get it merged to the library once it's confirmed as a reasonable addition. From your description so far there doesn't seem to be any harm in adding these instances especially if you're able to keep reasonable positions and consumed flags, so I would encourage you to open a pull request. However, if you would prefer for another maintainer to weigh in who can give you more concrete guidance then feel free to do so. Thanks! |
I've already written the |
What library provides the |
Ah ok, I had an idea it was Liam's, but Pursuit wasn't finding it. I'm fine with that since he's a core maintainer. |
Here’s the solution for this in terms of runParser csvinput do
datarows :: List (List String) <- tryRethrow do
allrows <- parseCSV
liftMaybe (\_ -> "CSV does not contain any data, only column names!") $ tail allrows If the “CSV contains some data” condition is not met, then the error position is at the beginning of the CSV, because of the This solution to the example seems pretty simple and general to me, so I think I'm going to say that #212 solves this issue without adding any new typeclasses to |
Yesterday i noticed that a
Filterable
instance could be useful forParserT
Let's say you want to sometimes discard a parse based on some condition or predicate. For example, let's say you have a
parseCSV :: Parser String (List (List String))
, which parses a CSV input to a list of lines (lines being lists of strings, one string per value between commas or newlines). The first row of the CSV is usually just all the column names, so let's say i want to remove that line usingtail :: forall a. List a -> Maybe (List a)
. I could then usefilterMap :: forall a b f. Filterable f => (a -> Maybe b) -> f a -> f b
to remove that like this:filterMap tail parseCSV
. The result of this would be a parser that ignores the first line of the CSV, and only gives the rest. It would also fail if the CSV input only has one line. Additionally, a custom error message can be provided like this (i think):filterMap tail parseCSV <?> "CSV does not contain any data, only column names!"
The implementation would be similar to the
Filterable
instance forMaybe
andEither
, in the sense that it's only got one item to filter (as opposed to a list).I could write up a PR for this, just wanted to hear some opinions first.
The text was updated successfully, but these errors were encountered: