Sort in return statement
Seb
seb at wilzba.ch
Sat Dec 9 07:32:42 UTC 2017
On Saturday, 9 December 2017 at 02:45:35 UTC, rjframe wrote:
> On Sat, 09 Dec 2017 02:34:29 +0000, codephantom wrote:
>
>> Anyone got ideas on how to get sort() working in the *return*
>> statement?
>>
>> //------------
>>
>> ushort[] draw8Numbers()
>> {
>> import std.meta : aliasSeqOf;
>> import std.range : iota;
>> ushort[] numbers = [ aliasSeqOf!(iota(1,46)) ];
>>
>> import std.random : randomShuffle;
>> randomShuffle(numbers);
>>
>> import std.range : take;
>> import std.algorithm.sorting : sort;
>> return numbers.take(8); /* ok */
>> //return sort(numbers.take(8)); /* I want this, but it
>> won't
>> work. */
>>
>> }
>>
>> // -------------
>
>
> `sort` returns a SortedRange of ushorts, not an array of
> ushorts. Make it:
>
> ```
> import std.array : array;
> return sort(numbers.take(8)).array;
> ```
>
> --Ryan
Use .release to obtain the underlying array. No need to do
another allocation!
```
numbers.take(8).sort.release;
```
More information about the Digitalmars-d-learn
mailing list