Input Range addition to associative arrays
Phil Lavoie
maidenphil at hotmail.com
Thu Dec 6 11:03:25 PST 2012
Hi,
I am aware that you can iterate over associative arrays in
multiple ways using the structure's opApply and different
delegates returned by properties. I think adding a "range"
property returning an input range to the associative array would
benefit the users of the D language.
I'd like to justify this addition by pointing out that some
standard library functional features, like filter, do not work on
associative arrays as of right now. The reason for this is that
filter returns a range that moves to the next appropriate value
ON DEMAND rather than constructing the result. I think it is a
great design choice. However, since no incremental (read on
demand) iteration can be done on associative arrays, one cannot
use features such as filter, nor make his own.
I think that the existing code can easily support such an
addition, which makes it all the more attractive.
Imagine that "front()" would return an entry such that entry.key
returns the key and entry.value returns the value. Here is an
example of how one could use it:
float[ item ] itemsCost;
//Items whose price are lower than 5 dollars.
auto myFavoriteItems = filter!"a.value < 5.0"( itemsCost );
foreach( cheapItem; myFavoriteItems ) {
theMoreComplicatedAlgorithmOnEarth( cheapItem); }
Now don't get me wrong, I am aware that you can do this multiple
ways. It's just that ranges are so attractive in the way they
"lazily" fetch values that it would make it easier to wrap
associative arrays in struct that return relevant ranges based on
the aa's content of constructing the result, than returning the
range/or result. I also think it would merge well with D's way of
doing things.
What are your thoughts?
More information about the Digitalmars-d
mailing list