<meta charset="utf-8"><span class="Apple-style-span" style="border-collapse: 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>> ==================<br>
> import std.algorithm;<br>><br>> public:<br>><br>> void remove(T)(ref T[] array, T element)<br>> {<br>> auto index = array.countUntil!("a == b", T[], T)(array, element);<br>> removeAt(index);<br>
> }<br>><br>> void removeAt(T)(ref T[] array, sizediff_t index)<br>> {<br>> if(index < 0 || index >= array.length)<br>> return;<br>> array.replaceInPlace(index, index + 1, []);<br>> }<br>
><br>><br>> unittest<br>> {<br>> auto a = [1, 3, 4];<br>> a.remove(3);<br>> assert(a == [1, 4]);<br>> }<br></span><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">> ======================</span><div>
<font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">Hi Lloyd,<br>
</span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">why not just use the built in functionality of arrays? <br>
</span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">//----------------------</span></font></div>
<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><div>import std.stdio;</div></span></font><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; "> </span><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><div>
void main() {</div><div> auto a = [1, 2, 3, 4, 5, 6, 7, 8];</div><div> remove(a, 3);</div><div> </div><div> foreach (i; a) {</div><div> write(i, " ");</div><div> }</div><div> writeln("");</div>
<div>}</div><div><br></div><div>void remove(T) (ref T[] myarray, int element) {</div><div> auto front = myarray[0 .. (element -1)];</div><div> auto back = myarray[element .. $];</div><div> myarray = front ~ back;</div>
<div> // or simply: myarray = myarray[0 .. (element - 1)] ~ myarray[element .. $];</div><div>}</div><div><meta charset="utf-8">//----------------------</div><div><br></div><div>Josh</div></span></font></div>