samol
samol

Reputation: 20600

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. What is the idiomatic way to write this in scala

Upvotes: 0

Views: 75

Answers (1)

jwvh
jwvh

Reputation: 51271

I think containsSlice() is what you're after.

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

Upvotes: 2

Related Questions