-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Simplifications for Parser #288
Comments
Parser.succeed (\_ y -> x) |= p
--> Parser.succeed (\y -> x) needs to be Parser.succeed (\_ y -> x) |= p
--> Parser.succeed (\y -> x) |. p (because it will still consume ignored parsers) And Parser.mapChompedString (always f) p
--> p should be Parser.mapChompedString (always f) p
--> Parser.map f p
-- or alternatively (probably less nice in this context)
--> Parser.succeed f |= p And maybe add Parser.mapChompedString (\string parsed -> ..string, parsed..) (Parser.succeed a)
--> Parser.succeed (.."", a..) |
|
As @wolfadex mentioned: |
is not a valid simplification. You'd lose the x and instead use result of p that was previously skipped. Maybe you meant |= instead of |.? |
Thanks all for the feedback and corrections! Please double-check I didn't mess up anything. @lue-bird I didn't get the last @miniBill Good points about @miniBill I've also changed the @Janiczek I don't think it works with I've also added |
An example of how the Parser.mapChompedString (\string parsed -> f string parsed) (Parser.succeed a)
--> Parser.succeed (f "" a) because the string will always be |
This was raised in the Slack thread as well, but: I think the myParser =
Parser.succeed {- value or function -}
{- |. or |= -} |
@Janiczek My idea (as suggested on Slack) is to only apply these changes "when there is only a single pipe in the expression" Examples: myParser0 =
Parser.succeed f
|. p
-->
myParser0 =
p
myParser1 =
Parser.succeed identity
|= p
-->
myParser1 =
p
-- Unchanged
myParser2 =
Parser.succeed f
|. p
|= q Would this not suit you? If so, do I understand correctly that you prefer: myParser4 =
Parser.succeed identity
|= Parser.token "/"
-- over
myParser4 =
Parser.token "/" I'd say the latter is always a simplification over the former. Or are there other scenarios that you're thinking of? |
A common pattern I tend to use is to use the "identity" part as a description, e.g. Parser.succeed (\name size -> File { name = name, size = size })
|. ...
|. ...
|= ...
|. ...
|. ...
|= (Parser.succeed (\size -> size)
|= Parser.int
)
|. ... without the explaining identity function, it gets pretty hard to read as you have to jump around the code to know what you're actually producing. To be fair nobody does e.g. |
As @lue-bird showed, having the un-simplified It's probably fine, it's just stylistic, but I know you do pay attention to these stylistic issues in some rules ("why would somebody not use this") and this is potentially one of them. The other rules seem non-controversial. |
@lue-bird Would a comment not serve the same purpose? Parser.succeed (\name size -> File { name = name, size = size })
|. ...
|. ...
|= ...
|. ...
|. ...
-- Size
|= Parser.int
|. ... Or by extracting it and giving it a name like
I absolutely agree with this. But I'd say a lot of simplifications already destroy this. For instance (and I know it's a bit odd, but for the sake of example), one could want to keep I'd be more in favor to not apply the simplification if there were some readability benefits.
@Janiczek Absolutely. I'm mostly trying to figure out whether there is a better approach that we can all agree on is better. If there is none or if there is no consensus, then I am very happy to drop this 😄 I guess where I come from here is that I find it hard to believe that Parser.succeed identity -- or with lambda
|= Parser.int is easier to read than just |
I've only found one example in my own code: Parser.succeed (\right -> BinaryOp left RangeInclusive right)
|> Parser.keep (Parser.lazy (\() -> exprAux precedence isRight)) I think I'd be OK with changing it into Parser.lazy (\() -> ...)
|> Parser.map (\right -> ...) if elm-review suggested that, even though it's not how I'd have written things... 👍 |
@Janiczek I'm confused, I don't think your example is one of the cases above? 🤔 |
@miniBill You can imagine it being Parser.succeed identity
|> Parser.keep (Parser.lazy (\() -> exprAux precedence isRight)) changing into Parser.lazy (\() -> ...) |
The latter is definitely something we should do, but your example with |
needs to be |
Parser.map
simplificationsParser.andThen
simplificationsParser.succeed () |. p
->p
p
is aParser ()
Parser.succeed identity |= p
->p
Parser.succeed x |. Parser.succeed y
->Parser.succeed x
Parser.succeed always |= p |= q
->Parser.succeed identity |. p |= q
Parser.succeed (\_ y -> x) |= p
->Parser.succeed (\y -> x) |. p
(Note, may need import of the|.
operator)Parser.chompIf (always False)
->Parser.succeed ()
Parser.chompWhile (always False)
->Parser.succeed ()
Parser.mapChompedString (always f) p
->Parser.map f p
Parser.getChompedString (Parser.succeed x)
->Parser.succeed ""
As far as I can tell, all of these would also apply to
Parser.Advanced
.The text was updated successfully, but these errors were encountered: