Is there a -​- (minus minus) operator in Scala?

Viewed 573

I've come across this operator in a Scala implementation of a graph (that you can find the example here, where two lists are interponed the operator -- and also two Maps.

abstract class GraphBase[T, U] {
  case class Edge(n1: Node, n2: Node, value: U) {
    def toTuple = (n1.value, n2.value, value)
  }
  case class Node(value: T) {
    var adj: List[Edge] = Nil
    // neighbors are all nodes adjacent to this node.
    def neighbors: List[Node] = adj.map(edgeTarget(_, this).get)
  }

  var nodes: Map[T, Node] = Map()
  var edges: List[Edge] = Nil

  // If the edge E connects N to another node, returns the other node,
  // otherwise returns None.
  def edgeTarget(e: Edge, n: Node): Option[Node]

  override def equals(o: Any) = o match {
    case g: GraphBase[_,_] => (nodes.keys.toList -- g.nodes.keys.toList == Nil &&
                               edges.map(_.toTuple) -- g.edges.map(_.toTuple) == Nil)
    case _ => false
  }
  def addNode(value: T) = {
    val n = new Node(value)
    nodes = Map(value -> n) ++ nodes
    n
  }
}

My current interpreter does not recognize it, so I am wondering where is this operator coming from? Does it mean list subtraction? Is it valid Scala code?

1 Answers

You can alter an implementation of equals method to avoid usage of --:

override def equals(o: Any) = o match {
  case g: GraphBase[_,_] => (nodes.keySet == g.nodes.keySet &&
                             edges.map(_.toTuple).toSet == g.edges.map(_.toTuple).toSet)
  case _ => false
}
Related