Re: replace() and replace-with()

My adventurous side says: Why not, let’s do it. My concerned voice
says: Won’t people be confused if we introduce too many concepts
simultaneously? People have just got used to use maps for options…

  replace($input, map { 'pattern': '[0-9]+', 'replacement' : '#$0' })

…so they might come back to us and ask what’s the difference between
the old and new syntax? Which is the preferred choice? What’s the
difference between ":=" and ":"? Why was the alternative added, if the
number of characters to be saved is minimal? And all that.

If we add the syntactic sugar for arguments with 4.0, I would vote for
at least using the old syntax in the XQFO 4.0 spec. The more I think
about it, the more I would stick with the existing bulky map
constructor syntax. Currently, we cannot differentiate yet between
arbitrary maps and maps with options, and by introducing the syntactic
sugar, we mix up two different concepts. Maybe we should tackle the
convenient syntax once the record/struct or the item type concepts are
finalized? Maybe we can then manage to only accept "option maps" to be
written down in the convenience notation?

Next to that, I don’t know if we should overload arguments with
different semantics, depending on the function arity. A new
replace-with function would thus be my favorite, and it promises to be
both more convenient and readable than expressions with
fn:analyze-string.
____________________________________

On Thu, Dec 3, 2020 at 8:19 AM Michael Kay <mike@saxonica.com> wrote:
>
> Dimitre's concerns about the naming of the proposed replace-with() function could also be addressed by combining replace() and replace-with() into a single new fn:replace#2 using keyword parameters:
>
> replace($input, pattern:="[0-9]+", replacement:="#$0")
>
> replace($input, pattern:="[0-9]+", replacement:="#$0", flags="ms")
>
> replace($input, pattern:="[0-9]+", using:=format-integer(?, 'Ww'))
>
> which might open the door to having a replacement function with access to matched subgroups:
>
> replace($input, pattern:="^([0-9]+)/([0-9]+)/([0-9]+)$", process-groups="map{
>    1: format-integer(?, '00'),
>    2: map{'1': 'January', '2': 'February', ...}
>    3: format-integer(?, '0000')
> })
>
> (the value of process-groups is a function that accepts a group number as input and returns a function used for processing that group; in this example we exploit the fact that a map is a function).
>
> Michael Kay
> Saxonica

Received on Thursday, 3 December 2020 10:30:24 UTC