digitalmars.D.learn - Re: simple syntax issue with template
- Joshua Niehus <jm.niehus gmail.com> Jun 13 2011
--001517741a9e10ae5d04a5999567 Content-Type: text/plain; charset=ISO-8859-1I'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; ">> I= 'm trying to create 2 extra method for arrays ("range" would = be<br> > better, though I don't quite understand what is a "range"= ;)<br>> Although I have some indecipherable (to me) compiler error...<br=><br>> What's wrong with the code below?<br>> =3D=3D=3D=3D=3D=
> import std.algorithm;<br>><br>> public:<br>><br>> void rem= ove(T)(ref T[] array, T element)<br>> {<br>> =A0 =A0auto index =3D ar= ray.countUntil!("a =3D=3D b", T[], T)(array, element);<br>> = =A0 =A0removeAt(index);<br> > }<br>><br>> void removeAt(T)(ref T[] array, sizediff_t index)<br=> {<br>> =A0 =A0if(index < 0 || index >=3D array.length)<br>&g=
+ 1, []);<br>> }<br> ><br>><br>> unittest<br>> {<br>> =A0 =A0auto a =3D [1, 3, 4]= ;<br>> =A0 =A0a.remove(3);<br>> =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; ">> =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, " ");</div><div>=A0 =A0= }</div><div>=A0 =A0 writeln("");</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