Re: Sorted iterator for mappings
Fredrik Hubinette <[email protected]>
| Newsgroups | gmane.comp.lang.pike.user |
|---|---|
| Message-ID | <CAKab2FwtTcMgkaF4S8-ajLUBHzFaUSCjvYVkntbJOEegYSh_mg@mail.gmail.com> |
There are alternatives, not sure if they are better though:
foreach(sort((array)some_map), [mixed key, mixed value])
Alternatively, we could implement a proper tree-based ADT.SortedMap
It seems like your sort_mapping is really a generic iterator sorter though,
so maybe
it should be named something like ADT.SortIterator() or something.
/Hubbe
On Tue, Nov 3, 2015 at 9:55 AM, Chris Angelico <[email protected]> wrote:
> Often I want to iterate over a mapping, for some kind of display
> purpose, and want to run through the elements in order. As far as I
> know, there's no easy way to do that at the moment, so I usually end
> up doing this:
>
> foreach (sort(indices(some_map)), mixed key)
>
> and then work with key and some_map[key]. In contrast, I can iterate
> in arbitrary order thus:
>
> foreach (some_map; mixed key; mixed value)
>
> It'd be nice to have a way to use iterator syntax, but with a
> guarantee that they're sorted. I've put this together as a
> proof-of-concept:
>
> https://github.com/Rosuav/shed/blob/master/sort_mapping.pike
>
> Is there a better way to do this?
>
> Could something like this possibly be added to the Mapping module?
>
> ChrisA
>
>
>