What is an idiomatic Scala way to "remove" one element from an immutable List?

ListScala

List Problem Overview


I have a List, which may contain elements that will compare as equal. I would like a similar List, but with one element removed. So from (A, B, C, B, D) I would like to be able to "remove" just one B to get e.g. (A, C, B, D). The order of the elements in the result does not matter.

I have working code, written in a Lisp-inspired way in Scala. Is there a more idiomatic way to do this?

The context is a card game where two decks of standard cards are in play, so there may be duplicate cards but still played one at a time.

def removeOne(c: Card, left: List[Card], right: List[Card]): List[Card] = {
  if (Nil == right) {
    return left
  }
  if (c == right.head) {
    return left ::: right.tail
  }
  return removeOne(c, right.head :: left, right.tail)
}

def removeCard(c: Card, cards: List[Card]): List[Card] = {
  return removeOne(c, Nil, cards)
}

List Solutions


Solution 1 - List

I haven't seen this possibility in the answers above, so:

scala> def remove(num: Int, list: List[Int]) = list diff List(num)
remove: (num: Int,list: List[Int])List[Int]

scala> remove(2,List(1,2,3,4,5))
res2: List[Int] = List(1, 3, 4, 5)

Edit:

scala> remove(2,List(2,2,2))
res0: List[Int] = List(2, 2)

Like a charm :-).

Solution 2 - List

You could use the filterNot method.

val data = "test"
list = List("this", "is", "a", "test")
list.filterNot(elm => elm == data)

Solution 3 - List

You could try this:

scala> val (left,right) = List(1,2,3,2,4).span(_ != 2)
left: List[Int] = List(1)
right: List[Int] = List(2, 3, 2, 4)

scala> left ::: right.tail                            
res7: List[Int] = List(1, 3, 2, 4)

And as method:

def removeInt(i: Int, li: List[Int]) = {
   val (left, right) = li.span(_ != i)
   left ::: right.drop(1)
}

Solution 4 - List

Unfortunately, the collections hierarchy got itself into a bit of a mess with - on List. For ArrayBuffer it works just like you might hope:

scala> collection.mutable.ArrayBuffer(1,2,3,2,4) - 2
res0: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 3, 2, 4)

but, sadly, List ended up with a filterNot-style implementation and thus does the "wrong thing" and throws a deprecation warning at you (sensible enough, since it is actually filterNoting):

scala> 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)

So arguably the easiest thing to do is convert List into a collection that does this right, and then convert back again:

import collection.mutable.ArrayBuffer._
scala> ((ArrayBuffer() ++ List(1,2,3,2,4)) - 2).toList
res2: List[Int] = List(1, 3, 2, 4)

Alternatively, you could keep the logic of the code you've got but make the style more idiomatic:

def removeInt(i: Int, li: List[Int]) = {
  def removeOne(i: Int, left: List[Int], right: List[Int]): List[Int] = right match {
    case r :: rest =>
      if (r == i) left.reverse ::: rest
      else removeOne(i, r :: left, rest)
    case Nil => left.reverse
  }
  removeOne(i, Nil, li)
}

scala> removeInt(2, List(1,2,3,2,4))
res3: List[Int] = List(1, 3, 2, 4)

Solution 5 - List

 def removeAtIdx[T](idx: Int, listToRemoveFrom: List[T]): List[T] = {
    assert(listToRemoveFrom.length > idx && idx >= 0)
    val (left, _ :: right) = listToRemoveFrom.splitAt(idx)
    left ++ right
 }

Solution 6 - List

How about

def removeCard(c: Card, cards: List[Card]) = {
  val (head, tail) = cards span {c!=}	
  head ::: 
  (tail match {
    case x :: xs => xs
    case Nil => Nil
  })
}

If you see return, there's something wrong.

Solution 7 - List

// throws a MatchError exception if i isn't found in li
def remove[A](i:A, li:List[A]) = {
   val (head,_::tail) = li.span(i != _)
   head ::: tail
}

Solution 8 - List

As one possible solutions you can find index of the first suitable element and then remove element at this index:

def removeOne(l: List[Card], c: Card) = l indexOf c match {
    case -1 => l
    case n => (l take n) ++ (l drop (n + 1))
}

Solution 9 - List

Just another thought on how to do this using a fold:

def remove[A](item : A, lst : List[A]) : List[A] = {
    lst.:\[List[A]](Nil)((lst, lstItem) => 
       if (lstItem == item) lst else lstItem::lst )
}

Solution 10 - List

Generic Tail Recursion Solution:

def removeElement[T](list: List[T], ele: T): List[T] = {
    @tailrec
    def removeElementHelper(list: List[T],
                            accumList: List[T] = List[T]()): List[T] = {
      if (list.length == 1) {
        if (list.head == ele) accumList.reverse
        else accumList.reverse ::: list
      } else {
        list match {
          case head :: tail if (head != ele) =>
            removeElementHelper(tail, head :: accumList)
          case head :: tail if (head == ele) => (accumList.reverse ::: tail)
          case _                             => accumList
        }
      }
    }
    removeElementHelper(list)
  }

Solution 11 - List

val list : Array[Int] = Array(6, 5, 3, 1, 8, 7, 2)
val test2 = list.splitAt(list.length / 2)._2
val res = test2.patch(1, Nil, 1)

Solution 12 - List

object HelloWorld {

    def main(args: Array[String]) {

        var months: List[String] = List("December","November","October","September","August", "July","June","May","April","March","February","January")

        println("Deleting the reverse list one by one")

        var i = 0

        while (i < (months.length)){

            println("Deleting "+months.apply(i))

            months = (months.drop(1))
        
        }

        println(months)

    }

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGavilan ComunView Question on Stackoverflow
Solution 1 - ListAntonin BrettsnajdrView Answer on Stackoverflow
Solution 2 - ListSøren MathiasenView Answer on Stackoverflow
Solution 3 - ListFrank S. ThomasView Answer on Stackoverflow
Solution 4 - ListRex KerrView Answer on Stackoverflow
Solution 5 - ListSuat KARAKUSOGLUView Answer on Stackoverflow
Solution 6 - ListEugene YokotaView Answer on Stackoverflow
Solution 7 - ListKen BloomView Answer on Stackoverflow
Solution 8 - ListtenshiView Answer on Stackoverflow
Solution 9 - ListgdizView Answer on Stackoverflow
Solution 10 - ListShankar ShastriView Answer on Stackoverflow
Solution 11 - ListHuckleberry FinnView Answer on Stackoverflow
Solution 12 - ListAnbinsonView Answer on Stackoverflow