如何在惯用Scala中进行序列比较

时间:2017-07-07 00:17:39

标签: scala

输入:

target: ["a", "b", "c", "d"]

sequence1: ["b", "c"]
sequence2: ["c", "b"]

期望的行为

sequence1 matches with target because "b", "c" matches with a sub sequence of the target

sequence2 does not match with target

我可以很容易地使用for循环在Python或Java中编写它。在scala中写这个的惯用方法是什么

1 个答案:

答案 0 :(得分:2)

我认为containsSlice()就是你所追求的目标。

scala> List("a","b","c","d").containsSlice(List("b","c"))
res0: Boolean = true

scala> List("a","b","c","d").containsSlice(List("c","b"))
res1: Boolean = false