Replies: 1 comment 4 replies
-
Why not?
I think this is rather readable. (You don't have to actually call In this case the parser for something like |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
For context, the
std/text/parser
library has parser combinators. But these combinators are unusual in the sense that they don't return functions, which can be passed into other combinators, but rather execute the combinator itself.This means that, in order to pass the result of a parser combinator to another parser combinator, one needs to wrap the previous combinator call in a lambda, before passing into the next combinator.
For example, I have the following to help parse an array of UUID strings
All this does is pass the UUID parser, in order, to a combinator which is
many
but with delimiters, drop the parsed delimiters, parse whitespace around the list, and parse all that wrapped in[]
.I found this is the most ergonomic way to write this logic, however, the order in which the parser combinators are applied is reversed, and If I were to want to do this within a larger function, I'd need to define a lambda containing the
with
s, and immediately call it. For example:While this could easily be fixed by making the parser combinators return parsers instead of the result of calling the parser, I feel this pattern is valid, and could be useful.
It would be nice if a new syntax sugar operator, say
=>
(better operator names could be chosen), which does the same thing as the.
operator, but wraps the expression before it in a lambda.This way the logic could be written as (what looks like) chained method calls.
And could be used within a bigger function without explicitly wrapping the inner expression in a lambda
More concretely, the operator would desugar any expression
a=>b(c,d,...)
into({a}).b(c,d,...)
, which, of course, would eventually desugar intob(({a}),c,d,...)
Beta Was this translation helpful? Give feedback.
All reactions