simple syntax issue with template

Joshua Niehus jm.niehus at gmail.com
Mon Jun 13 08:29:16 PDT 2011


> I'm trying to create 2 extra method for arrays ("range" would be
> better, though I don't quite understand what is a "range")
> Although I have some indecipherable (to me) compiler error...
>
> What's wrong with the code below?
> ==================
> import std.algorithm;
>
> public:
>
> void remove(T)(ref T[] array, T element)
> {
>    auto index = array.countUntil!("a == b", T[], T)(array, element);
>    removeAt(index);
> }
>
> void removeAt(T)(ref T[] array, sizediff_t index)
> {
>    if(index < 0 || index >= array.length)
>        return;
>    array.replaceInPlace(index, index + 1, []);
> }
>
>
> unittest
> {
>    auto a = [1, 3, 4];
>    a.remove(3);
>    assert(a == [1, 4]);
> }
> ======================

Hi Lloyd,

why not just use the built in functionality of arrays?

//----------------------
import std.stdio;

void main() {
    auto a = [1, 2, 3, 4, 5, 6, 7, 8];
    remove(a, 3);

    foreach (i; a) {
        write(i, " ");
    }
    writeln("");
}

void remove(T) (ref T[] myarray, int element) {
    auto front = myarray[0 .. (element -1)];
    auto back =  myarray[element .. $];
    myarray = front ~ back;
    // or simply: myarray = myarray[0 .. (element - 1)] ~ myarray[element ..
$];
}
//----------------------

Josh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110613/4e4fc6a2/attachment.html>


More information about the Digitalmars-d-learn mailing list