-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add a function to replace matches with a callback #3
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice, thank you!
I'm not sure about the name still. replace_map
sounds like it's removing a "map", or mapping over something after it has been replaced.
Is there a verb we could use?
Replacement1 = fun(Content, Submatches) -> | ||
Submatches1 = gleam@list:map(Submatches, fun gleam@string:to_option/1), | ||
Replacement({match, Content, Submatches1}) | ||
end, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this use the same submatches function above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to use it and it didn't work. The function signature for the replacement is fn((binary(), [binary()]) -> iodata() | unicode:charlist())
so the submatches are just [binary()]
.
} | ||
let regexMatch = new RegexMatch(match, List.fromArray(submatches)); | ||
return replacement(regexMatch); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a copy/paste of the code above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's very similar, but not identical. I originally pulled it out into its own function to reuse, but when I did, my test failed. I didn't dig in to understand why the above code iterated backwards, but when I iterated forwards, the test passed.
I'm not opposed to trying to find a way to unify the two, but I didn't want to go altering existing code that works without discussing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at it again, I bet I didn't account for the n - 1
in the scan implementation since the match
array in scan
has the whole match string as the first element, whereas this array only holds the submatches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems a shame there's such similar code here! Can they not be unified?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they can. I'll take a stab at it later today.
I'll keep thinking on the name, but I don't have any good ideas at the moment. I tried looking at other languages without overloading for inspiration, but only saw |
|
That seems good to me. |
closes #2
I've gone back-and-forth between the names
replace_match
andreplace_map
, but I don't think either are great.