Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Unfortunately, the collections hierarchy got itself into a bit of a mess with <code>-</code> on <code>List</code>. For <code>ArrayBuffer</code> it works just like you might hope:</p> <pre><code>scala&gt; collection.mutable.ArrayBuffer(1,2,3,2,4) - 2 res0: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 3, 2, 4) </code></pre> <p>but, sadly, <code>List</code> ended up with a <code>filterNot</code>-style implementation and thus does the "wrong thing" <em>and</em> throws a deprecation warning at you (sensible enough, since it is actually <code>filterNot</code>ing):</p> <pre><code>scala&gt; List(1,2,3,2,4) - 2 warning: there were deprecation warnings; re-run with -deprecation for details res1: List[Int] = List(1, 3, 4) </code></pre> <p>So arguably the easiest thing to do is convert <code>List</code> into a collection that does this right, and then convert back again:</p> <pre><code>import collection.mutable.ArrayBuffer._ scala&gt; ((ArrayBuffer() ++ List(1,2,3,2,4)) - 2).toList res2: List[Int] = List(1, 3, 2, 4) </code></pre> <p>Alternatively, you could keep the logic of the code you've got but make the style more idiomatic:</p> <pre><code>def removeInt(i: Int, li: List[Int]) = { def removeOne(i: Int, left: List[Int], right: List[Int]): List[Int] = right match { case r :: rest =&gt; if (r == i) left.reverse ::: rest else removeOne(i, r :: left, rest) case Nil =&gt; left.reverse } removeOne(i, Nil, li) } scala&gt; removeInt(2, List(1,2,3,2,4)) res3: List[Int] = List(1, 3, 2, 4) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload