Jump to page: 1 2 3
Thread overview
Why can't static arrays be sorted?
Oct 04, 2016
TheGag96
Oct 04, 2016
Vladimir Panteleev
Oct 04, 2016
Adrian Matoga
Oct 05, 2016
Jonathan M Davis
Oct 05, 2016
TheGag96
Oct 05, 2016
pineapple
Oct 05, 2016
Jonathan M Davis
Oct 05, 2016
Ali Çehreli
Oct 06, 2016
pineapple
Oct 06, 2016
pineapple
Oct 08, 2016
notna
Oct 06, 2016
Adrian Matoga
Oct 06, 2016
TheGag96
Oct 06, 2016
ag0aep6g
Oct 08, 2016
Jon Degenhardt
Oct 10, 2016
TheGag96
Oct 10, 2016
Jonathan M Davis
passing static arrays to each! with a ref param [Re: Why can't static arrays be sorted?]
Oct 11, 2016
Jon Degenhardt
Oct 11, 2016
ag0aep6g
Oct 11, 2016
Jon Degenhardt
Oct 11, 2016
Jon Degenhardt
October 04, 2016
I was writing some code today and ran into this oddity that I'd never come across before:

    import std.algorithm : sort;
    int[10] arr = [0, 3, 4, 6, 2, 1, 1, 4, 6, 9];
    thing.sort();

This doesn't compile. Obviously the .sort property works, but what about static arrays makes them unable to be sorted by std.algorithm.sort? Thanks.
October 04, 2016
On Tuesday, 4 October 2016 at 20:05:15 UTC, TheGag96 wrote:
> I was writing some code today and ran into this oddity that I'd never come across before:
>
>     import std.algorithm : sort;
>     int[10] arr = [0, 3, 4, 6, 2, 1, 1, 4, 6, 9];
>     thing.sort();
>
> This doesn't compile. Obviously the .sort property works, but what about static arrays makes them unable to be sorted by std.algorithm.sort? Thanks.

Static arrays in D are value types. Try:

     thing[].sort();

This will pass a slice of the array to sort, holding a reference to the static array's data.
October 04, 2016
On Tuesday, 4 October 2016 at 20:05:15 UTC, TheGag96 wrote:
> I was writing some code today and ran into this oddity that I'd never come across before:
>
>     import std.algorithm : sort;
>     int[10] arr = [0, 3, 4, 6, 2, 1, 1, 4, 6, 9];
>     thing.sort();
>
> This doesn't compile. Obviously the .sort property works, but what about static arrays makes them unable to be sorted by std.algorithm.sort? Thanks.


Try:
arr[].sort();
October 04, 2016
On Tuesday, October 04, 2016 20:05:15 TheGag96 via Digitalmars-d-learn wrote:
> I was writing some code today and ran into this oddity that I'd never come across before:
>
>      import std.algorithm : sort;
>      int[10] arr = [0, 3, 4, 6, 2, 1, 1, 4, 6, 9];
>      thing.sort();
>
> This doesn't compile. Obviously the .sort property works, but what about static arrays makes them unable to be sorted by std.algorithm.sort? Thanks.

The problem is that static arrays aren't ranges (calling popFront on them can't work, because their length isn't mutable). However, you can slice a static array to get a dynamic array which _is_ a range. e.g.

thing[].sort();

Just make sure that such a dynamic array does not outlive the static array, or it will refer to invalid memory (which would not be a problem in this case).

- Jonathan M Davis

October 05, 2016
On Wednesday, 5 October 2016 at 02:19:13 UTC, Jonathan M Davis wrote:
> The problem is that static arrays aren't ranges (calling popFront on them can't work, because their length isn't mutable). However, you can slice a static array to get a dynamic array which _is_ a range. e.g.
>
> thing[].sort();
>
> Just make sure that such a dynamic array does not outlive the static array, or it will refer to invalid memory (which would not be a problem in this case).
>
> - Jonathan M Davis

Ah thanks guys. I think I just got used to thinking arrays would always be found to be ranges by the compiler. Good to know!
October 05, 2016
On Wednesday, 5 October 2016 at 18:19:27 UTC, TheGag96 wrote:
> On Wednesday, 5 October 2016 at 02:19:13 UTC, Jonathan M Davis wrote:
>> The problem is that static arrays aren't ranges (calling popFront on them can't work, because their length isn't mutable). However, you can slice a static array to get a dynamic array which _is_ a range. e.g.
>>
>> thing[].sort();
>>
>> Just make sure that such a dynamic array does not outlive the static array, or it will refer to invalid memory (which would not be a problem in this case).
>>
>> - Jonathan M Davis
>
> Ah thanks guys. I think I just got used to thinking arrays would always be found to be ranges by the compiler. Good to know!

Would just like to point out that this is design weirdness on Phobos' part - the library I've been writing does not have this problem.
October 05, 2016
On Wednesday, October 05, 2016 19:22:03 pineapple via Digitalmars-d-learn wrote:
> On Wednesday, 5 October 2016 at 18:19:27 UTC, TheGag96 wrote:
> > On Wednesday, 5 October 2016 at 02:19:13 UTC, Jonathan M Davis
> >
> > wrote:
> >> The problem is that static arrays aren't ranges (calling popFront on them can't work, because their length isn't mutable). However, you can slice a static array to get a dynamic array which _is_ a range. e.g.
> >>
> >> thing[].sort();
> >>
> >> Just make sure that such a dynamic array does not outlive the static array, or it will refer to invalid memory (which would not be a problem in this case).
> >>
> >> - Jonathan M Davis
> >
> > Ah thanks guys. I think I just got used to thinking arrays would always be found to be ranges by the compiler. Good to know!
>
> Would just like to point out that this is design weirdness on Phobos' part - the library I've been writing does not have this problem.

It doesn't even make conceptual sense for a static array to be a range, because you can't remove elements from it.

- Jonathan M Davis

October 05, 2016
On 10/05/2016 12:30 PM, Jonathan M Davis via Digitalmars-d-learn wrote:
> On Wednesday, October 05, 2016 19:22:03 pineapple via Digitalmars-d-learn
> wrote:

>> Would just like to point out that this is design weirdness on
>> Phobos' part - the library I've been writing does not have this
>> problem.
>
> It doesn't even make conceptual sense for a static array to be a range,
> because you can't remove elements from it.

But algorithms like sort() need not require popFront(). I guess we need another range type: NonShrinkingRandomAccessRange. :)

Ali

October 06, 2016
On Wednesday, 5 October 2016 at 19:30:01 UTC, Jonathan M Davis wrote:
>> Would just like to point out that this is design weirdness on Phobos' part - the library I've been writing does not have this problem.
>
> It doesn't even make conceptual sense for a static array to be a range, because you can't remove elements from it.
>
> - Jonathan M Davis

Just because the static array itself isn't a range doesn't mean that it should be necessary to do unintuitive gymnastics with it just to pass it to functions like `sort`.

October 06, 2016
On Thursday, 6 October 2016 at 09:17:08 UTC, pineapple wrote:
> On Wednesday, 5 October 2016 at 19:30:01 UTC, Jonathan M Davis wrote:
>>> Would just like to point out that this is design weirdness on Phobos' part - the library I've been writing does not have this problem.
>>
>> It doesn't even make conceptual sense for a static array to be a range, because you can't remove elements from it.
>>
>> - Jonathan M Davis
>
> Just because the static array itself isn't a range doesn't mean that it should be necessary to do unintuitive gymnastics with it just to pass it to functions like `sort`.

I mean, people post here how often asking why static or dynamic arrays aren't being accepted by Phobos' range functions in their code?

Maybe Phobos really ought to consider another approach. Accepting things that are _valid_ as ranges and not only things that are ranges themselves has proven to be an effective strategy in mach.

« First   ‹ Prev
1 2 3