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

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

A minor difference is that array:for-each returns an array, while "for
member" allows you to process array members more flexibly.

While I appreciate the flexibility that would be introduced with the
“members” keyword, I am not convinced if the processing of arrays will
get common enough to justify such an extension (even if we provide
better native support for this type). Thus, if we add a “member”
keyword, I would agree with Reece that we should treat both maps
(which are pretty common today) and arrays equal by including an
additional “pair” or “entry” keyword. If we decide not to, we could
still add a simple array:members() function, which returns each member
of an array as new array:

  let $array := [ 1, 2, (3, 4) ]
  for $member in array:members($array)
  return <_>{ $member?* }</_>

…would then yield…

  <_>1</_>
  <_>2</_>
  <_>3 4</_>

…and this function could also be used outside FLWOR expressions:

  array:members($array) ! <_>{ ?* }</_>

I would love to see this function, or a similar, added, even if we
enhance FLWOR expressions.

In addition, I believe that map:values() and array:values() would be
rewarding alternatives to the compact but Perly "?*" syntax. There are
nice use cases for this notation, but often, a plain function would
definitely be more readable (and it would e.g. prevent people from
using array:flatten() when it’s not appropriate).



> 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 22:11:06 UTC