www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Re: simple syntax issue with template

--001517741a9e10ae5d04a5999567
Content-Type: text/plain; charset=ISO-8859-1

 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 --001517741a9e10ae5d04a5999567 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <meta charset=3D"utf-8"><span class=3D"Apple-style-span" style=3D"border-co= llapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">&gt; I= &#39;m trying to create 2 extra method for arrays (&quot;range&quot; would = be<br> &gt; better, though I don&#39;t quite understand what is a &quot;range&quot= ;)<br>&gt; Although I have some indecipherable (to me) compiler error...<br=
&gt;<br>&gt; What&#39;s wrong with the code below?<br>&gt; =3D=3D=3D=3D=3D=

&gt; import std.algorithm;<br>&gt;<br>&gt; public:<br>&gt;<br>&gt; void rem= ove(T)(ref T[] array, T element)<br>&gt; {<br>&gt; =A0 =A0auto index =3D ar= ray.countUntil!(&quot;a =3D=3D b&quot;, T[], T)(array, element);<br>&gt; = =A0 =A0removeAt(index);<br> &gt; }<br>&gt;<br>&gt; void removeAt(T)(ref T[] array, sizediff_t index)<br=
&gt; {<br>&gt; =A0 =A0if(index &lt; 0 || index &gt;=3D array.length)<br>&g=

+ 1, []);<br>&gt; }<br> &gt;<br>&gt;<br>&gt; unittest<br>&gt; {<br>&gt; =A0 =A0auto a =3D [1, 3, 4]= ;<br>&gt; =A0 =A0a.remove(3);<br>&gt; =A0 =A0assert(a =3D=3D [1, 4]);<br>&g= t; }<br></span><span class=3D"Apple-style-span" style=3D"border-collapse: c= ollapse; font-family: arial, sans-serif; font-size: 13px; ">&gt; =3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</span><div> <font class=3D"Apple-style-span" face=3D"arial, sans-serif"><span class=3D"= Apple-style-span" style=3D"border-collapse: collapse;"><br></span></font></= div><div><font class=3D"Apple-style-span" face=3D"arial, sans-serif"><span = class=3D"Apple-style-span" style=3D"border-collapse: collapse;">Hi Lloyd,<b= r> </span></font></div><div><font class=3D"Apple-style-span" face=3D"arial, sa= ns-serif"><span class=3D"Apple-style-span" style=3D"border-collapse: collap= se;"><br></span></font></div><div><font class=3D"Apple-style-span" face=3D"= arial, sans-serif"><span class=3D"Apple-style-span" style=3D"border-collaps= e: collapse;">why not just use the built in functionality of arrays?=A0<br> </span></font></div><div><font class=3D"Apple-style-span" face=3D"arial, sa= ns-serif"><span class=3D"Apple-style-span" style=3D"border-collapse: collap= se;"><br></span></font></div><div><font class=3D"Apple-style-span" face=3D"= arial, sans-serif"><span class=3D"Apple-style-span" style=3D"border-collaps= e: collapse;">//----------------------</span></font></div> <div><font class=3D"Apple-style-span" face=3D"arial, sans-serif"><span clas= s=3D"Apple-style-span" style=3D"border-collapse: collapse;"><div>import std= .stdio;</div></span></font><span class=3D"Apple-style-span" style=3D"border= -collapse: collapse; font-family: arial, sans-serif; ">=A0 =A0</span><font = class=3D"Apple-style-span" face=3D"arial, sans-serif"><span class=3D"Apple-= style-span" style=3D"border-collapse: collapse;"><div> void main() {</div><div>=A0 =A0 auto a =3D [1, 2, 3, 4, 5, 6, 7, 8];</div><= div>=A0 =A0 remove(a, 3);</div><div>=A0 =A0=A0</div><div>=A0 =A0 foreach (i= ; a) {</div><div>=A0 =A0 =A0 =A0 write(i, &quot; &quot;);</div><div>=A0 =A0= }</div><div>=A0 =A0 writeln(&quot;&quot;);</div> <div>}</div><div><br></div><div>void remove(T) (ref T[] myarray, int elemen= t) {</div><div>=A0 =A0 auto front =3D myarray[0 .. (element -1)];</div><div=
=A0 =A0 auto back =3D =A0myarray[element .. $];</div><div>=A0 =A0 myarray =

<div>=A0 =A0 // or simply: myarray =3D myarray[0 .. (element - 1)] ~ myarra= y[element .. $];</div><div>}</div><div><meta charset=3D"utf-8">//----------= ------------</div><div><br></div><div>Josh</div></span></font></div> --001517741a9e10ae5d04a5999567--
Jun 13 2011