monnef
monnef

Reputation: 4053

The "--" operator for list subtraction in Scala

In "S-99: Ninety-Nine Scala Problems" they use -- on a List in graph's equals method. The problem is, that in Scala I use (2.10.2), the -- operator isn't present (or I'm missing some import).

scala> List(1) -- List(1)
<console>:8: error: value -- is not a member of List[Int]
              List(1) -- List(1)
                      ^

Expected result is empty list.

In older versions of Scala it was working fine (according to this post).

Is there a subtract operator for Lists in Scala's standard library or do I need to cook one myself?

Upvotes: 29

Views: 25609

Answers (1)

kiritsuku
kiritsuku

Reputation: 53348

scala> List(1,2,3,4) filterNot List(1,2).contains
res2: List[Int] = List(3, 4)

or

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

Upvotes: 68

Related Questions