Scala 中列表减法的“--”运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18800447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The "--" operator for list subtraction in Scala
提问by monnef
In "S-99: Ninety-Nine Scala Problems" they use --on a Listin graph's equalsmethod. The problem is, that in Scala I use (2.10.2), the --operator isn't present (or I'm missing some import).
在“ S-99:九十九个 Scala 问题”中,他们使用--了List图的equals方法。问题是,在我使用的 Scala (2.10.2) 中,--操作符不存在(或者我缺少一些导入)。
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).
在旧版本的 Scala 中它运行良好(根据这篇文章)。
Is there a subtract operator for Lists in Scala's standard library or do I need to cook one myself?
ListScala 的标准库中是否有s的减法运算符,还是我需要自己做一个?
回答by kiritsuku
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)

