简体   繁体   English

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

[英]How to do sequence comparison in idiomatic Scala

Input: 输入:

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

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

Desired behavior 期望的行为

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

sequence2 does not match with target

I can write this in Python or Java with a for loop pretty easily. 我可以很容易地使用for循环用Python或Java编写此代码。 What is the idiomatic way to write this in scala 用Scala编写代码的惯用方式是什么

I think containsSlice() is what you're after. 我认为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM