Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The "--" operator for list subtraction in Scala

Tags:

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?

like image 981
monnef Avatar asked Sep 14 '13 09:09

monnef


People also ask

What does >> mean in Scala?

The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand.

How to check for not equal to in Scala?

In Scala, you can check if two operands are equal ( == ) or not ( != ) and it returns true if the condition is met, false if not ( else ). By itself, ! is called the Logical NOT Operator.

What is a list in Scala?

Specific to Scala, a list is a collection which contains immutable data, which means that once the list is created, then it can not be altered. In Scala, the list represents a linked list. In a Scala list, each element need not be of the same data type.

What is the difference between :: and #:: In Scala What is the difference between ::: and in Scala?

In general: :: - adds an element at the beginning of a list and returns a list with the added element. ::: - concatenates two lists and returns the concatenated list.


1 Answers

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) 
like image 144
kiritsuku Avatar answered Sep 19 '22 21:09

kiritsuku