Re: how to make the commented out lines work when they are uncommented

2015-11-02 Thread Ali Çehreli via Digitalmars-d-learn

On 11/02/2015 08:43 PM, steven kladitis wrote:
> import std.stdio, std.algorithm, std.array, std.traits,std.range,
> std.typecons,std.complex;
>
> enum AreSortableArrayItems(T) = isMutable!T &&
>  __traits(compiles, T.init < T.init) &&
>  !isNarrowString!(T[]);
>
> void selectionSort(T)(T[] data) if (AreSortableArrayItems!T) {
>  foreach (immutable i, ref d; data)
>  data.drop(i).minPos[0].swap(d);
> }

> //char[] a4 = ['a', 'b'];
> //a4.selectionSort;

That doesn't compile because selectionSort() requires !isNarrowString 
but char[] is a narrow string. If you are fine with sorting those bytes, 
then try ubyte[] as the type. Otherwise, sorting chars is dubious 
because char is a Unicode code unit, potentially a part of a Unicode 
character.


> //auto a7 = [complex(1.0,0.0), complex(2.0,0.0)];
> //a7.selectionSort;

That fails because there is no default ordering between complex numbers. 
(Ditto for a8.) (I haven't looked at the implementation of 'complex' but 
I remember from Math that that doesn't make sense anyway.)


Ali



how to make the commented out lines work when they are uncommented

2015-11-02 Thread steven kladitis via Digitalmars-d-learn
import std.stdio, std.algorithm, std.array, std.traits,std.range, 
std.typecons,std.complex;


enum AreSortableArrayItems(T) = isMutable!T &&
__traits(compiles, T.init < 
T.init) &&

!isNarrowString!(T[]);

void selectionSort(T)(T[] data) if (AreSortableArrayItems!T) {
foreach (immutable i, ref d; data)
data.drop(i).minPos[0].swap(d);
}
void main() {
auto a = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2];
a.selectionSort;
a.writeln;
int[] a0;
a0.selectionSort;
a0.writeln;

auto a1 = [1];
a1.selectionSort;
a1.writeln;

auto a2 = ["a", "b"];
a2.selectionSort;
a2.writeln;

auto a3 = ["b", "a"];
a3.selectionSort;
a3.writeln;

//char[] a4 = ['a', 'b'];
//a4.selectionSort;
//a4.writeln;

dstring[] a5 = ["b", "a"];
a5.selectionSort;
a5.writeln;


alias Nullable!int N;
auto a6 = [N(2), N(1)];
a6.selectionSort; // Not nothrow.
a6.writeln;

//auto a7 = [complex(1.0,0.0), complex(2.0,0.0)];
//a7.selectionSort;
//a7.writeln;


//  auto a8 = [complex(1.0L), complex(2.0L)];
//a8.selectionSort;
//a8.writeln;


static struct F {
int x;
int opCmp(F f) { // Not pure.
return x < f.x ? -1 : (x > f.x ? 1 : 0);
}
}
auto a9 = [F(2), F(1)];
a9.selectionSort;
a9.writeln;
}
// dmd 2.069 rc2