Safer casts
Janice Caron
caron800 at googlemail.com
Sat May 10 11:47:29 PDT 2008
On 10/05/2008, Dee Girl <deegirl at noreply.com> wrote:
> Nice example! How did you do it? Did Tango change the compiler and added more methods to arrays? Thank you, Dee Girl
Yeah, but look what you can do with Phobos.
import std.algorithm;
int[] array = [ 1, 2, 3, 4 ];
// sort in descending order
sort!("a > b")(array);
assert(array == [ 4, 3, 2, 1 ]);
// sort in ascending order
sort(array);
assert(array == [ 1, 2, 3, 4 ]);
// sort with a delegate
bool myComp(int x, int y) { return x > y; }
sort!(myComp)(array);
assert(array == [ 4, 3, 2, 1 ]);
// Showcase stable sorting
string[] words = [ "aBc", "a", "abc", "b", "ABC", "c" ];
sort!("toupper(a) < toupper(b)", SwapStrategy.stable)(words);
assert(words == [ "a", "aBc", "abc", "ABC", "b", "c" ]);
More information about the Digitalmars-d
mailing list