| Thread overview | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 01, 2014 Associative Ranges | ||||
|---|---|---|---|---|
| ||||
I just curious, do Associative Ranges exist. If so where can i find them. I started thinking about them when i asked this question: http://forum.dlang.org/thread/vauuognmhvtjrktazyeb@forum.dlang.org | ||||
August 02, 2014 Re: Associative Ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Freddy | On Friday, 1 August 2014 at 23:57:37 UTC, Freddy wrote:
> I just curious, do Associative Ranges exist. If so where can i
> find them. I started thinking about them when i asked this
> question:
> http://forum.dlang.org/thread/vauuognmhvtjrktazyeb@forum.dlang.org
Unfortunately not, but I do wonder if we should generalize an interface for these types of ranges.
| |||
August 02, 2014 Re: Associative Ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Xinok | Xinok:
> I do wonder if we should generalize an interface for these types of ranges.
First of all you need some use cases and usage examples.
Bye,
bearophile
| |||
August 02, 2014 Re: Associative Ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Saturday, 2 August 2014 at 14:49:49 UTC, bearophile wrote:
> Xinok:
>
>> I do wonder if we should generalize an interface for these types of ranges.
>
> First of all you need some use cases and usage examples.
>
> Bye,
> bearophile
The most obvious use case is generic functions that can operate on associative ranges of any type, regardless of implementation. There are various ways to implement and optimize hash tables for specific use cases, so it would be convenient not to be restricted to a single container.
As for usage examples, I don't have any because I'm not sure what the primitives should be. I will emphasize that "associative ranges" should be distinguishable from random-access ranges. If they weren't, then we would have to comb through Phobos and add checks everywhere, e.g. (!isAssociativeRange!Range). I don't think forward ranges and bidirectional ranges would be an issue though.
| |||
August 02, 2014 Re: Associative Ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Xinok | On Saturday, 2 August 2014 at 15:46:31 UTC, Xinok wrote:
> On Saturday, 2 August 2014 at 14:49:49 UTC, bearophile wrote:
>> Xinok:
>>
>>> I do wonder if we should generalize an interface for these types of ranges.
>>
>> First of all you need some use cases and usage examples.
>>
>> Bye,
>> bearophile
>
> The most obvious use case is generic functions that can operate on associative ranges of any type, regardless of implementation. There are various ways to implement and optimize hash tables for specific use cases, so it would be convenient not to be restricted to a single container.
>
> As for usage examples, I don't have any because I'm not sure what the primitives should be. I will emphasize that "associative ranges" should be distinguishable from random-access ranges. If they weren't, then we would have to comb through Phobos and add checks everywhere, e.g. (!isAssociativeRange!Range). I don't think forward ranges and bidirectional ranges would be an issue though.
It might be a ploblem if you have an Associative Range where the
key is size_t unless Associative ranges are not Input/Forward
Ranges
| |||
August 03, 2014 Re: Associative Ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Freddy | On Friday, 1 August 2014 at 23:57:37 UTC, Freddy wrote: > I just curious, do Associative Ranges exist. If so where can i > find them. I started thinking about them when i asked this > question: > http://forum.dlang.org/thread/vauuognmhvtjrktazyeb@forum.dlang.org I started a phobos fork for this, what do you think so far https://github.com/Superstar64/phobos/blob/60d3472b1056b298319976f105aa3b9b3f165e97/std/range.d#L1357-1420 | |||
August 03, 2014 Re: Associative Ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Freddy | On Sunday, 3 August 2014 at 06:19:12 UTC, Freddy wrote:
> On Friday, 1 August 2014 at 23:57:37 UTC, Freddy wrote:
>> I just curious, do Associative Ranges exist. If so where can i
>> find them. I started thinking about them when i asked this
>> question:
>> http://forum.dlang.org/thread/vauuognmhvtjrktazyeb@forum.dlang.org
> I started a phobos fork for this, what do you think so far
> https://github.com/Superstar64/phobos/blob/60d3472b1056b298319976f105aa3b9b3f165e97/std/range.d#L1357-1420
Nice!
A few comments after a cursory glance:
1) "aligns" sounds strange, use "corresponds" instead.
2) It would be preferable that associative ranges mimic built-in associative arrays as closely as possible, i.e. the range members should be called `byKey` and `byValue`, and it should allow iteration over the associative range directly, instead of using the `range` member.
3) For the value and key ranges, there should be a guarantee that they can be zipped through, i.e. that the elements in them are in the same order so keys and values correspond to each other. The built-in associative arrays provide `byKey` and `byValue`, which satisfy this condition.
| |||
August 03, 2014 Re: Associative Ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Sunday, 3 August 2014 at 08:50:47 UTC, Marc Schütz wrote:
> On Sunday, 3 August 2014 at 06:19:12 UTC, Freddy wrote:
>> On Friday, 1 August 2014 at 23:57:37 UTC, Freddy wrote:
>>> I just curious, do Associative Ranges exist. If so where can i
>>> find them. I started thinking about them when i asked this
>>> question:
>>> http://forum.dlang.org/thread/vauuognmhvtjrktazyeb@forum.dlang.org
>> I started a phobos fork for this, what do you think so far
>> https://github.com/Superstar64/phobos/blob/60d3472b1056b298319976f105aa3b9b3f165e97/std/range.d#L1357-1420
>
> Nice!
>
> A few comments after a cursory glance:
>
> 1) "aligns" sounds strange, use "corresponds" instead.
>
> 2) It would be preferable that associative ranges mimic built-in associative arrays as closely as possible, i.e. the range members should be called `byKey` and `byValue`, and it should allow iteration over the associative range directly, instead of using the `range` member.
>
> 3) For the value and key ranges, there should be a guarantee that they can be zipped through, i.e. that the elements in them are in the same order so keys and values correspond to each other. The built-in associative arrays provide `byKey` and `byValue`, which satisfy this condition.
Alright i fixed it, although should the implict range be a
forward range, an input range or something else and how do you
implement front,popFront, and empty on a built-in associative
array.
| |||
August 03, 2014 Re: Associative Ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Sunday, 3 August 2014 at 08:50:47 UTC, Marc Schütz wrote:
> On Sunday, 3 August 2014 at 06:19:12 UTC, Freddy wrote:
>> On Friday, 1 August 2014 at 23:57:37 UTC, Freddy wrote:
>>> I just curious, do Associative Ranges exist. If so where can i
>>> find them. I started thinking about them when i asked this
>>> question:
>>> http://forum.dlang.org/thread/vauuognmhvtjrktazyeb@forum.dlang.org
>> I started a phobos fork for this, what do you think so far
>> https://github.com/Superstar64/phobos/blob/60d3472b1056b298319976f105aa3b9b3f165e97/std/range.d#L1357-1420
>
> Nice!
>
> A few comments after a cursory glance:
>
> 1) "aligns" sounds strange, use "corresponds" instead.
>
> 2) It would be preferable that associative ranges mimic built-in associative arrays as closely as possible, i.e. the range members should be called `byKey` and `byValue`, and it should allow iteration over the associative range directly, instead of using the `range` member.
>
> 3) For the value and key ranges, there should be a guarantee that they can be zipped through, i.e. that the elements in them are in the same order so keys and values correspond to each other. The built-in associative arrays provide `byKey` and `byValue`, which satisfy this condition.
Also won't the implicit range make it hard to implement function
like map (should you map to whole range or to the value)
| |||
August 04, 2014 Re: Associative Ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Freddy | On Sunday, 3 August 2014 at 18:38:51 UTC, Freddy wrote:
> On Sunday, 3 August 2014 at 08:50:47 UTC, Marc Schütz wrote:
>> 3) For the value and key ranges, there should be a guarantee that they can be zipped through, i.e. that the elements in them are in the same order so keys and values correspond to each other. The built-in associative arrays provide `byKey` and `byValue`, which satisfy this condition.
> Also won't the implicit range make it hard to implement function
> like map (should you map to whole range or to the value)
I guess when you iterate over it, you just get tuples. So, `map` would return a normal range of tuples, not an associative range. There could then be a method to convert such a range to an associative range (although it would need to assume that the first tuple elements = keys are unique).
I'm not sure whether this is the right thing to do. In any case I don't think it's good to map the keys, because they mustn't be modified, or otherwise you could get duplicate keys.
An alternative would be that `map` takes both a the key and the value, but returns only the new value. The same goes for the other range operations (e.g. reduce). But on the other hand, this doesn't seem to scale, because all range algorithms would then have to be specialized for associative ranges...
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply