digitalmars.D.learn - Functional Sort
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (5/5) Nov 14 2014 Is there a functional variant of std.algorithm.sort, say sorted,
- Meta (3/8) Nov 14 2014 `sort` returns a SortedRange, so sort is the function you're
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (3/5) Nov 14 2014 Do you mean std.algorithm.sort?
- Meta (4/9) Nov 14 2014 In that case, just .dup the array before sorting, as you want a
- Meta (3/15) Nov 14 2014 Sorry, and if you want a copy, just add a `.array` on the end to
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (4/8) Nov 14 2014 Great!
- Meta (3/12) Nov 14 2014 There's only std.array.array. I think std.algorithm just
- Steven Schveighoffer (7/21) Nov 14 2014 err... this isn't what you want. That will sort the range, and then make...
- Meta (6/8) Nov 15 2014 Yes, I didn't see the the second constraint to not sort the
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (8/16) Nov 15 2014 BTW: When I uncomment line
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (10/11) Nov 15 2014 I solved it by replacing
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (16/21) Nov 15 2014 Does this mean that r.array is better than my current
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?
Nov 14 2014
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.
Nov 14 2014
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.
Nov 14 2014
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: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.`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.
Nov 14 2014
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:Sorry, and if you want a copy, just add a `.array` on the end to create a new array from the returned range.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.
Nov 14 2014
On Saturday, 15 November 2014 at 00:47:57 UTC, Meta wrote:Great! Should I use std.algorithm.array or std.array.array in these cases?`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.
Nov 14 2014
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:There's only std.array.array. I think std.algorithm just publically imports std.array.Great! Should I use std.algorithm.array or std.array.array in these cases?`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.
Nov 14 2014
On 11/14/14 7:47 PM, Meta wrote:On Saturday, 15 November 2014 at 00:45:11 UTC, Meta 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. 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 :) -SteveOn Saturday, 15 November 2014 at 00:33:11 UTC, Nordlöw wrote:Sorry, and if you want a copy, just add a `.array` on the end to create a new array from the returned range.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.
Nov 14 2014
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.
Nov 15 2014
On Saturday, 15 November 2014 at 08:52:45 UTC, Meta wrote:On Saturday, 15 November 2014 at 03:47:25 UTC, Steven Schveighoffer wrote:BTW: When I uncomment line /* assert(x.sorted == y); */ in string unittest it errors as sort_ex.d(160,5): Error: can only sort a mutable array sort_ex.d(178,13): Error: template instance sort_ex.sorted!string error instantiating What's wrong with my isArray-overload of sorted?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.
Nov 15 2014
On Saturday, 15 November 2014 at 14:34:07 UTC, Nordlöw wrote:What's wrong with my isArray-overload of sorted?I solved it by replacing R s = r.dup; with auto s = r.dup; As a follow up I know wonder if it is ok for isArray-overload of sorted() to have return type ubyte[] if input is a string? I wonder because x.array.sort has type dchar[] when x is a string.
Nov 15 2014
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 :) -SteveDoes 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 ?
Nov 15 2014