Thread overview |
---|
November 15, 2014 Functional Sort | ||||
---|---|---|---|---|
| ||||
Is there a functional variant of std.algorithm.sort, say sorted, that returns a sorted copy of its input use typically as const y = x.sorted; ? If not any recommendations on its implementation? |
November 15, 2014 Re: Functional Sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Saturday, 15 November 2014 at 00:33:11 UTC, Nordlöw wrote:
> Is there a functional variant of std.algorithm.sort, say sorted, that returns a sorted copy of its input use typically as
>
> const y = x.sorted;
>
> ?
>
> If not any recommendations on its implementation?
`sort` returns a SortedRange, so sort is the function you're looking for.
|
November 15, 2014 Re: Functional Sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Saturday, 15 November 2014 at 00:45:11 UTC, Meta wrote:
> `sort` returns a SortedRange, so sort is the function you're looking for.
Do you mean std.algorithm.sort?
I want a sort that doesn't mutate its input argument.
|
November 15, 2014 Re: Functional Sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Saturday, 15 November 2014 at 00:45:11 UTC, Meta wrote:
> On Saturday, 15 November 2014 at 00:33:11 UTC, Nordlöw wrote:
>> Is there a functional variant of std.algorithm.sort, say sorted, that returns a sorted copy of its input use typically as
>>
>> const y = x.sorted;
>>
>> ?
>>
>> If not any recommendations on its implementation?
>
> `sort` returns a SortedRange, so sort is the function you're looking for.
Sorry, and if you want a copy, just add a `.array` on the end to create a new array from the returned range.
|
November 15, 2014 Re: Functional Sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Saturday, 15 November 2014 at 00:47:41 UTC, Nordlöw wrote:
> On Saturday, 15 November 2014 at 00:45:11 UTC, Meta wrote:
>> `sort` returns a SortedRange, so sort is the function you're looking for.
>
> Do you mean std.algorithm.sort?
>
> I want a sort that doesn't mutate its input argument.
In that case, just .dup the array before sorting, as you want a copy anyway. I don't think there's a sorting function in Phobos that doesn't mutate its argument.
|
November 15, 2014 Re: Functional Sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Saturday, 15 November 2014 at 00:47:57 UTC, Meta wrote:
>> `sort` returns a SortedRange, so sort is the function you're looking for.
>
> Sorry, and if you want a copy, just add a `.array` on the end to create a new array from the returned range.
Great!
Should I use std.algorithm.array or std.array.array in these cases?
|
November 15, 2014 Re: Functional Sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Saturday, 15 November 2014 at 01:01:57 UTC, Nordlöw wrote:
> On Saturday, 15 November 2014 at 00:47:57 UTC, Meta wrote:
>>> `sort` returns a SortedRange, so sort is the function you're looking for.
>>
>> Sorry, and if you want a copy, just add a `.array` on the end to create a new array from the returned range.
>
> Great!
>
> Should I use std.algorithm.array or std.array.array in these cases?
There's only std.array.array. I think std.algorithm just publically imports std.array.
|
November 15, 2014 Re: Functional Sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On 11/14/14 7:47 PM, Meta wrote:
> On Saturday, 15 November 2014 at 00:45:11 UTC, Meta wrote:
>> On Saturday, 15 November 2014 at 00:33:11 UTC, Nordlöw wrote:
>>> Is there a functional variant of std.algorithm.sort, say sorted, that
>>> returns a sorted copy of its input use typically as
>>>
>>> const y = x.sorted;
>>>
>>> ?
>>>
>>> If not any recommendations on its implementation?
>>
>> `sort` returns a SortedRange, so sort is the function you're looking for.
>
> Sorry, and if you want a copy, just add a `.array` on the end to create
> a new array from the returned range.
err... this isn't what you want. That will sort the range, and then make a copy of the sorted range as an array.
Note, there isn't any generic way to say "give me a copy of this range, as the same type." array is probably the best you will get. Just make sure you call it *before* you sort, unless you want both ranges sorted :)
-Steve
|
November 15, 2014 Re: Functional Sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Saturday, 15 November 2014 at 03:47:25 UTC, Steven Schveighoffer wrote:
> err... this isn't what you want. That will sort the range, and then make a copy of the sorted range as an array.
Yes, I didn't see the the second constraint to not sort the original range.
Sort before .array -> original will be sorted.
Sort after .array -> original will not be sorted.
|
November 15, 2014 Re: Functional Sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Saturday, 15 November 2014 at 03:47:25 UTC, Steven Schveighoffer wrote: > Note, there isn't any generic way to say "give me a copy of this range, as the same type." array is probably the best you will get. Just make sure you call it *before* you sort, unless you want both ranges sorted :) > > -Steve Does this mean that r.array is better than my current auto sorted(R)(const R r) if (isInputRange!R && !(isArray!R)) { alias E = ElementType!R; import std.algorithm: sort, copy; auto s = new E[r.length]; // TODO length is probably not available here r.copy(s); s.sort; return s; } at https://github.com/nordlow/justd/blob/master/sort_ex.d#L117 ? |
Copyright © 1999-2021 by the D Language Foundation