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

On Thu, 17 Dec 2020 at 22:10, Christian Grün <cg@basex.org> wrote:

> > 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.
>

Yes, that's true. FLWOR expressions also allow filtering (when), grouping,
sorting, and position to be used in a single expression (which would apply
to both arrays and maps).


> 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 would be happy with functions to get the sequence of members/entries from
an array or map instead of a specific `for member` syntax. Other languages
provide both a map:values and a map:entries function in addition to the
existing map:keys function, so that and having a non-recursive
array:flatten would be useful.

Kind regards,
Reece

Received on Thursday, 17 December 2020 22:33:28 UTC