-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make RegexMatch iterable #34355
Make RegexMatch iterable #34355
Conversation
Needs News.md. May warrent mentioning in Docs? |
I think you'll also need to define: length(m::RegexMatch) = length(m.captures) |
Also Reading the test code, it looks like the plan is to treat |
I thought the idea was to treat |
I was just looking at this new test Lines 139 to 147 in e6794e1
I think |
This PR makes it iterable. The bit this adds is much less debatable. |
This makes sense to me although I can't say I've encountered the motivation to iterate over the captures of a single regex match. Can you give a sense of what kind of code needs this? Allowing indexing by index also seems like a fine addition to me. Why not, after all? |
I often want to write code that looks like this test:
Indexing by index? We've always allowed that. |
Then what is the proposed indexing by? |
We are not adding indexing at all? |
I'm asking about that. |
There's a bit of thinking to be done here: if a RegexMatch object is map from indices or symbols to captures, then shouldn't it iterate like that dictionary? If you want a vector of captures, you can already get that by accessing |
Ok, so you are talking to @tkf fair enough. From my point of view: I would have do to While I very often want the capture values to be passed to a function, I have never wanted the capture names. I always know those since i named it. |
Why not just pass the |
Issue with that is can't access Dispatching into I have a bunch of code that does;
and I think the destructuring is much cleaner |
I see. That makes sense. I guess I always just write out my |
I think a challenge for the Dict-like route is that it's not super clear what keys part should be for mixed named and unnamed captures like |
I think it is a collection of captures that is indexed by position (int) and optionally by name. |
Ok, that makes sense. I just wanted to clarify the data model. Does this complete the implementation of that data model? |
|
5cb04bd
to
f39528f
Compare
To return to this PR. |
Something that isn't great about |
I don't see that this needs to solve every problem to be a good idea. I don't know that i have ever used conditional captures. Further for conditional capture won't you want to write multiple different methods? foo((cap1,)::RegexMatch) =...
foo((cap1, cap2)::RegexMatch) = ...
foo((cap1, cap2, cap3)::RegexMatch) = ... This will also play nice with: #2626 (comment) then we will be able to have: foo((cap1, caps...)::RegexMatch)) = ... |
I must say the use-case shown in the NEWS looks pretty forced / unnatural to me. The code in #34355 (comment) looks perfectly fine and is easy to understand while the NEWS one is not. |
I'll need to make a correction on my part. I thought that alternative capture groups
This statement does seem to be correct to me. When mixing named and positional capture groups the groups are always in the order in which they were defined: julia> match(r"((?<b>b)|(?<a>a)(\n)?)", "a")
RegexMatch("a", 1="a", b=nothing, a="a", 4=nothing)
These all currently define the same method so |
Oh, good point. That does limit the utility of this. As a bit of an aside: AFAIK |
FYI I think another example is
I don't think we need to "fix" these cases, though. |
I continue to think we should do this.
@StefanKarpinski asked:
Yes, combined with #37300 (merged) this implements the complete data model to act like a (partially) named-tuple. @KristofferC said:
Should I remove that example from the NEWS.md, and just simply say that regex matches now iterate to give the captures? |
I have now done this |
bump |
don't forget to pass the state add length and eltype to RegexMatch Fix missing enumerate =update news for RegexMatch iterating fix typo in news Co-authored-by: Curtis Vogt <[email protected]> remove example of possible use from NEWS.md Remove stray at-test
add iterate, length, and eltype to RegexMatch Co-authored-by: Curtis Vogt <[email protected]>
add iterate, length, and eltype to RegexMatch Co-authored-by: Curtis Vogt <[email protected]>
Look at the tests.
Isn't this a nice feature?
In general its weird to define
getindex
without matchingiterate
.