-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arrays and maps: Members, entries, values, contents, pairs, … #1338
Comments
In #1457 (comment), I found cases in which “map keys” and “array indexes” are mentioned. I think it gets more and more confusing to respect the differences between maps and arrays, and analogous to |
I think that if we're going to contemplate adding If we're expecting the reader to build a mental model of arrays as a sequence of values and maps as a set of key/value pairs, then I worry that adding functions that make them more uniform just makes them harder to understand. I wonder if there'd be some substantial simplification possible if we just accepted that arrays are maps with sequential, integer keys. All the functions that apply to |
I could imagine that this would be a huge change and affect virtually every expression and function that handles arrays. It might as well introduce backward incompatibilities, as arrays are stricter data types than maps (with additional bound and type checks). But your idea sounds enticing; maybe I’m overly cautious. My intent in the scope of this issue would be mostly to get as consistent as possible, even if we cannot roll back the 3.1 decision to treat maps and arrays differently: If we want to provide a |
It's an appealing idea but the devil is in the detail. While it's true that all functions that access maps could be made to view arrays as maps with integer keys, the same isn't true for construction, and therefore it isn't true for operations (such as filtering and mapping) that combine retrieval access and construction. There's also the detail that array keys are naturally sorted. |
Superseded by #1871. |
With version 4.0, we are adding a lot of promising and powerful new map and array features. This is a big step forward, compared to the obvious limitations of 3.1.
Some aspects of the 3.1 design have made it difficult (or impossible) to fully adjust array and maps, but (in my opinion) the old overall concept was impressively consistent – and it is definitely a big challenge to achieve a 4.0 design that is not too fragmented.
To me, this becomes particularly evident in the case of arrays. The following example sums up the items of all members of an array. For the cumbersome 3.1 solution…
…we now have at least several (roughly?) equivalent options to do this; for example…
for member $m in $array return sum($m)
array:members($array) ! sum(?value)
$array?entry::* ! sum(?value)
$array?value::* ! sum(.)
…which is great – but the downside is that we have introduced a terminological jungle. The examples above could imply that:
array:entries
);array:value
returns a different structure).Next, with the current proposals,
$array:content::1
gives us the sequence-concatenated version of the first member of an array. Similar observations can be made with maps:map:entries($map)
returns singleton maps, whereas$map?entry::*
is actually equivalent tomap:pairs
.The fundamental obstacle are clear have already been discussed a lot, but I think that with each new concept, we should try really hard not to blur terminology, and work with terms that users can assign to the underlying concepts without too much guessing or trial’n’error.
My general suggestions would be to…
My concrete proposals:
map:pairs
,$map-or-array?entry::*
should become$map-or-array?pair::*
, and we should add aarray:pairs
function, and probablyarray:of-pairs
(see 77 Lookup returning path selection #832). We shouldn’t do it the other way round and renamemap:pairs
tomap:entries
, as the existingmap:entry
function returns a singleton map.(array|map):values
should be renamed to(array|map):contents
(see Editorial:array:values
,map:values
#1179).array:value::*
, we should make clear what an “array value” is, how it it positions itself in relation to an “array member”, and we should addmap:values
andarray:values
for equivalent results.array:key::*
, we should add aarray:keys
function (which returns a dense integer range).1 to array:size($array)
could then be written asarray:keys($array)
.map:entries
andmap:merge
, we could add equivalentarray:entries
andarray:merge
functions.array:members
/array:of-members
in favor of eitherarray:split
/array:join
,array:pairs
/array:of-pairs
(see 1.) orarray:entries
/array:merge
(see 5). I really believe that an “array member“ should not be a map; an “array pair” or ”array entry” certainly could.One might question if we should really introduce map terminology for arrays. I think we have no other chance if we want to treat maps and arrays identically with lookup key specifiers, and it may help us later on to treat both data structures as similar as possible.
The text was updated successfully, but these errors were encountered: