Re: replace() and replace-with()

On Wed, Dec 2, 2020 at 11:19 PM 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).
>
"


I am totally confused :(

Can you answer these questions, please:


   1. Is this definition of one function or of four functions? If this is a
   definition of one function, then how do we know that we have seen all of
   the definitions (maybe there are more than 4 definitions, but they are
   scattered across the code and we cannot be aware of all of them)?

   2. What is the arity of the function: 2 or 3 or 4 or 5 or 6? If the
   answer is 2, how can you explain to a developer that in all the above cases
   they are calling a 2-argument function, when they in fact are literally
   specifying 3 and 4 arguments?

   3. What if some of the keyword arguments are mutually exclusive and thus
   shouldn't be specified together on the same function call? Or if we have
   two different subsets of keyword arguments that are mutually exclusive? How
   can one specify this formally?

I personally prefer not to alter the old syntax of function declaration and
function call. When we want, we can specify that an argument is a map (or
more precisely a record). This is clear and won't lead to confusion. Also,
the question about the arity has a consistent answer -- in this particular
case the arity will be 3, not 2.

Such deep changes need scrupulous attention and discussion, so, please, go
gradually, patiently, step by step, and only proceed deeper if you have got
a near-consensus at the current step.

As for the name of the new function, I am comfortable with  replace-lifted()
or  replace-using-function() .

Thanks,
Dimitre

Received on Thursday, 3 December 2020 15:39:28 UTC