Allow MAP! indexing?

Carl Sassenrath, CTO
REBOL Technologies
29-May-2008 17:43 GMT

Article #0137
Main page || Index || Prior Article [0136] || Next Article [0138] || 9 Comments || Send feedback

The MAP! datatype is a dictionary, providing a hashed access storage method.

Recently, we extended MAP! to allow integers as keys (in addition to words and strings). This is handy to manage sparse arrays.

For example:

m: make map! 100
m/1: "this"
m/10: "is"
m/500: "an"
m/1234567: "example"

>> print [m/1 m/10 m/500 m/1234567]
== this is an example

Ok, agreed, that kind of thing can be very useful.

However, now we've lost the ability to ever access the map as a series, independent of its key values. We now get:

>> first m
== "this"
>> pick m 1
== "this"
>> pick m 2
== none

Is this a desirable result, or did we just lose something?

Note that we can still use the select function to obtain values from keys:

>> select m 1
== "this"

So, for maps, pick is a synonym for select. They are the same.

But should they be?

There are times when I want to obtain the first value of a MAP! to use as a default value for something. At other times, it would be nice to be able to iterate over the MAP!, even though we are not guaranteed the order of the elements. And, to be safe, we may only allow index by number, not by series offsets (more research is required there.)

Although it is true we can use to-block to return the map as a block, for a simple index access that operation is not efficient.

Your thoughts?


Updated 23-Mar-2017 - Edit - Copyright REBOL Technologies -