You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
seq provides a convenient way to return a nicely structured result (and close enough, if not exactly what you want most of the time), namely by using it with kwargs.
e.g.
It would be nice if there was a similarly convenient way to return something analogous for alternatives (i.e. alt or the | operator), the result of which is encodable as a tagged union/is an encoding of a sum type.
I don't see a way to use this together with an operator without hacks like passing it tuples or something (like (name, parser) | (name,parser)), but i think this would still be nice to have for the alt() form.
defalt(*parsers, **kw_parsers):
ifnotparsersandnotkw_parsers:
returnfail('<empty alt>')
ifparsersandkw_parsers:
raiseValueError("Use either positional arguments or keyword arguments with alt, not both")
ifparsers:
@Parserdefalt_parser(stream, index):
result=Noneforparserinparsers:
result=parser(stream, index).aggregate(result)
ifresult.status:
returnresultreturnresultreturnalt_parserelse:
@Parserdefalt_kwarg_parser(stream, index):
result=Noneforname, parserinkw_parsers.items():
result=parser(stream, index).aggregate(result)
ifresult.status:
#I'm not actually sure if this is the correct way to use aggregate here, I haven't looked at how it's supposed to workreturnResult.success(result.index, { name: result.value }).aggregate(result)
returnresultreturnalt_kwarg_parser
I happen to have been using(/my rationale for using) dicts (was) because it's lets me directly, somewhat meaningfully address the component of the result that I want, as opposed to using some "arbitrary" numerical index, which might possibly (?) change.
I don't know right now which should be done, or if it's possible to have both.
@deliciouslytyped Could you give a full example of what you are thinking of? The kind of thing that could potentially go in our docs? For me at least that would help assess whether this would fit into the existing patterns we are encouraging in parsy.
One of the purposes of this is to be able to pass to combine/combine_dict - see docs - as you suggest.
Regarding building this into alt, I'm wondering whether is particular value in having another way to do the same thing? Without proper sum types in Python, it's difficult to know exactly what pattern to encourage for building something that emulates them.
Activity
deliciouslytyped commentedon Dec 9, 2019
A thrown together example, based on
seq:(also needs the >= 3.6 check for ordered kwargs)
jneen commentedon Dec 12, 2019
I would prefer
(name, value)for this, but it seems reasonable to me!jneen commentedon Dec 12, 2019
Reason being: you could follow it up with
combine, likealt(a=..., b=...).combine(Node)and would receiveNode(type, value)deliciouslytyped commentedon Dec 13, 2019
I'm making this up as I go and use parsy more.
What you said sounds reasonable.
I happen to have been using(/my rationale for using) dicts (was) because it's lets me directly, somewhat meaningfully address the component of the result that I want, as opposed to using some "arbitrary" numerical index, which might possibly (?) change.
I don't know right now which should be done, or if it's possible to have both.
spookylukey commentedon Dec 17, 2019
@deliciouslytyped Could you give a full example of what you are thinking of? The kind of thing that could potentially go in our docs? For me at least that would help assess whether this would fit into the existing patterns we are encouraging in parsy.
jneen commentedon Dec 17, 2019
if i understand correctly, it's something like:
jneen commentedon Dec 17, 2019
without pattern matching, it's difficult for python to actually make use of those tuple values, but those seem more correct in general.
jneen commentedon Dec 17, 2019
i mean, something hacky like this would work, though.
spookylukey commentedon Dec 17, 2019
Thanks @jneen for the explanation.
Currently we have the
tagmethod that can do something very similar:One of the purposes of this is to be able to pass to
combine/combine_dict- see docs - as you suggest.Regarding building this into
alt, I'm wondering whether is particular value in having another way to do the same thing? Without proper sum types in Python, it's difficult to know exactly what pattern to encourage for building something that emulates them.deliciouslytyped commentedon Dec 17, 2019
Sorry, I'm super tired right now, I think my use of dicts might have been minimally analogous to pattern matching, but I'm not sure.
deliciouslytyped commentedon Dec 17, 2019
Ok
taglooks quite relevant. On the other hand you can choose to not tag some elements of the alternative. I'm not sure if that's desirable.seqdoes force using one or the other of positional/kwargs, but not mixing.lheckemann commentedon Apr 10, 2025
I think using a NamedTuple or dataclass and
mapwould be the best approach nowadays? Something like:(and I think this issue can be closed?)