October 27, 2010 [Issue 5124] New: Make std.algorithm.sort weakly pure | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=5124 Summary: Make std.algorithm.sort weakly pure Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: Phobos AssignedTo: nobody@puremagic.com ReportedBy: bearophile_hugs@eml.cc --- Comment #0 from bearophile_hugs@eml.cc 2010-10-26 17:20:57 PDT --- A pure sort allows to sort data even in strongly pure functions, this will be useful. Using DMD 2.050beta I have modified a little many Phobos functions and now the following testing code works: import std.algorithm: sort; import std.traits: FunctionAttribute, functionAttributes; alias FunctionAttribute FA; // shorten the enum name pure int[] foo1(int[] arr) { sort(arr); return arr; } pure int[] foo2(int[] arr) { sort!q{ a > b }(arr); return arr; } pure bool myComp1(int x, int y) { return x > y; } pure int[] foo3(int[] arr) { sort!(myComp1)(arr); return arr; } pure int[] foo4(int[] arr) { static pure bool myComp2(int x, int y) { return x > y; } // static assert(functionAttributes!(myComp2) & FA.PURE); // asserts //sort!(myComp2)(arr); return arr; } void main() { assert(foo1([5, 1, 7, 4]) == [1, 4, 5, 7]); assert(foo2([5, 1, 7, 4]) == [7, 5, 4, 1]); assert(foo3([5, 1, 7, 4]) == [7, 5, 4, 1]); //assert(foo4([5, 1, 7, 4]) == [7, 5, 4, 1]); } To make it compile I have had to turn pure many functions and change few things. Here are some of the changes: ------------------- std.functional, line 179: pure Body!(ElementType1, ElementType2).ReturnType This is possible and safe because the functions defined as strings are always static. ------------------- A problem is in std.algorithm, an assert calls text() that may become pure, but it will require some more work: Commented out line 5167 of std.algorithm: //assert(isSorted!lessFun(r), text(Range.stringof, ": ", r)); ------------------- Change the return type of std.range.assumeSorted, line 5405: pure SortedRange!(R, pred) assumeSorted(alias pred = "a < b", R)(R r) Note: pure auto assumeSorted(alias pred = "a < b", R)(R r) Can't be used because of bug 3934 (see also bug 5006 ) ------------------- SortedRange needs a pure this(): struct SortedRange(Range, alias pred = "a < b") if(isRandomAccessRange!(Unqual!Range)) { alias Unqual!Range R; private R _input; pure this(R input) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation