On Saturday, 9 December 2017 at 03:24:52 UTC, codephantom wrote:
On Saturday, 9 December 2017 at 02:45:35 UTC, rjframe wrote:
`sort` returns a SortedRange of ushorts, not an array of
ushorts. Make it:
```
import std.array : array;
return sort(numbers.take(8)).array;
```
--Ryan
That's it!
Thanks Ryan.
Yes, this works, and your algorithm would even accept arbitary
random-access ranges, not merely arrays.
But since we start explicitly with a ushort[] that this function
has allocated just for this algorithm, we could save the extra
allocation by the final call to array().
// ushort[] numbers = ...
randomShuffle(numbers);
import std.algorithm.sorting : sort;
numbers = numbers[0 .. 8];
sort(numbers);
return numbers;
sort(numbers) does two things: (1) affect the underlying data,
(2) return an input range with extra information that this
returned range is sorted. But in our example, we don't need to
allocate a fresh array from (2). We can return the sorted data
from (1), this is already in array-form.
-- Simon