Re: Allowing the for member syntax to enumerate key/value pairs in a map.

On Thu, 17 Dec 2020 at 20:34, Michael Kay <mike@saxonica.com> wrote:

> I'm reluctant. I don't think this requirement is frequent enough to
> justify custom syntax. What's wrong with
>
> map:for-each($map, ->($key, $value){``[`{$key}`=`{$value}`]``}
>
> Higher-order functions mean we can do an awful lot more without ad-hoc
> syntax.
>

Wouldn't that be a case against the `for member` syntax for arrays as well?
You could equally write array-based expressions like:

    array:for-each($array, ->($value){ $value * $value })

I personally find the FLWOR syntax easier to read than a higher-order
function, but can see where both could have their uses.

It also completes the symmetry between sequences, arrays, and maps in that
you will be able to use FLWORExpr/ForExpr expressions to enumerate the
values in any of those types (while still having the ability to use
higher-order functions if you want to use those). Otherwise, maps would be
the odd ones out in that you wouldn't be able to easily access the items in
the map.

Kind regards,
Reece

Michael Kay
> Saxonica
>
> On 17 Dec 2020, at 20:24, Reece Dunn <msclrhd@googlemail.com> wrote:
>
> Given the new `for member` syntax support for enumerating over the items
> in an array, it would be useful to have an easy way of enumerating over the
> key/value pairs in a map. This could be done through the same `for member`
> syntax, but the members for a map would be a 2-item sequence or a map of
> the key/value pairs in the map in an implementation-defined stable order.
>
> If the variant of this proposal to use a map for the key/value pairs is
> used, then given a map of type `map(K, V)` the member RecordTest would be
> `record(key as K, value as V)`. For a `map(*)` type, the member RecordTest
> would be `record(key, value)`.
>
> Combined with sequence/map/array decomposition, this would allow a user to
> write expressions like:
>
>     for member ${key, value} in $map
>     return ``[`{$key}`=`{value}`]``
>
> It may be useful to have a different keyword to denote maps, such as
> `entry` like JavaScript and Java, or `item` like Python, but using `member`
> for both arrays and maps would be more consistent with the user wanting to
> enumerate over the content of those data structures.
>
> Kind regards,
> Reece
>
>
>

Received on Thursday, 17 December 2020 20:53:57 UTC