How to check whether a sequence contains a given subsequence?

I can easily check whether a sequence contains a single element:

let a = [1, 2, 3, 4]
if a.contains(2) {
    print("a contains 2")
}

but what I want to do is to check whether it contains a subsequence:

let a = [1, 2, 3, 4]
if a.contains([2, 3]) { // <-- NOTE: Non-existing method.
    print("a contains [2, 3]")
}

Is there any existing API for this?

No, but my proposal and future PR exist :) count & contains

Feel free to use the implementation in the proposal, but it isn't optimized for Hashable types yet.

1 Like