-
-
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
replace: allow transform function as first argument? #24598
Comments
I would like to try to tackle this. But where would I find the source code for handling the |
See this section: https://docs.julialang.org/en/latest/manual/functions/#Do-Block-Syntax-for-Function-Arguments-1. You don't need to do anything special for |
(For another, non-string extension of |
@rfourquet, @nalimilan: does non-string extension collide with this change as far as you can tell? |
It wouldn't collide strictly speaking since this one would dispatch on It could be made more general so that it can return either |
This is exactly what #22324 does, with less general cases being defined in terms of this one (cf. the diff, where it is documented in terms of |
And if we go with this more general API, this should obviously wait till @nalimilan's work of replacing |
Ah, right, the function I described is just a convenience helper based on the |
From what I could find I don't think the branch replacing I don't understand the comment
Shouldn't whether this new |
I don't think we need the |
@twistedcubic Yes, it's been merged several months ago, see #23642. But we've decided against using |
Thanks. I have been experimenting with variations of:
But have been running into issues with
such as
I'm going to chew on it more. In the meanwhile:
|
Honestly I'm not so sure it's a good idea to add yet another method to the already crowded BTW, we could probably deprecate |
@StefanKarpinski and @JeffBezanson thoughts on whether we should keep pursuing allowing a transform function as the first arg? |
Maybe, this wouldn't add enough to warrant the added complexity. replace("The quick foxes run quickly.", r"quick|slow" => function(s)
uppercase(s)
end)
replace("The quick foxes run quickly.", r"quick|slow") do s
uppercase(s)
end But I think this presents an opportunity to enable something more powerful, in a backwards-compatible way. replace("The quick foxes run quickly.", r"quick|slow" => uppercase)
# The QUICK foxes run QUICKly.
replace("The quick foxes run quickly.", r"fox(es)?" => s"bus\1")
# The quick buses run quickly. I propose to add a method suitable for replace("The quick foxes run quickly.", r"fox(es)") do match
"bus" * uppercase(match[1])
end
# The quick busES run quickly. This would be a non-breaking change that adds more capabilities instead of just a new usage variant. Personally, I use the equivalent functionality in other languages from time to time. Of course, other standard libraries (cf. Python, JavaScript, Rust, .NET) don't bother to generalize their replace functions to other collections and I don't know whether string replacements are so much more useful to justify a special case. But I would say that Here's a slightly less trivial example of how this might be useful: template = "the {animal} is {activity|upper}"
variables = Dict("animal" => "fox", "activity" => "running")
transformations = Dict("|upper" => uppercase)
replace(template, r"{(.*?)(\|.*?)?}") do match
value = get(variables, match[1], "")
t = get(transformations, match[2], x->x)
t(value)
end
# the fox is RUNNING I found an example in Franklin.jl that does basically something like that and could be cleaned up and made more performant with this method, i.e. one pass instead of compiling a new regex and executing |
Issue #36293 proposes an alternative solution. |
The
replace
function allows a string, ans"..."
replacement specifier or a function as its third argument. For the sake ofdo
syntax, it might make sense to support the function as the first argument, like so:If someone decides to do this, while they're at it, an explanation of replacement patterns – i.e.
s"..."
strings and some more examples in thereplace
docs wouldn't hurt. E.g.:The text was updated successfully, but these errors were encountered: