My goal is to have a function available through all Sequence
like Array or Set.
This function should return a Bool
telling whether there is at least one object present in both sequences or not.
// Usage
let s1 = ["hi", "hey", "ho"]
let s2:Set = ["woop", "oi", "yes"]
if s2.intersects(with: s1) {
print("Happy me, it's a match")
}
extension Sequence where Element:Equatable {
func intersects<T:Sequence>(with anotherSequence:T) -> Bool
where T.Element: Equatable {
// ⬇ error: `Extraneous argument label 'where:' in call`
return self.contains(where: anotherSequence.contains)
}
}
// doing the same function outside works:
let rez = s1.contains(where: s2.contains)
print(rez)
I feel like I'm almost there but I don't understand why the first contains(where:)
gives me this error.
Both contains()
and contains(where:)
belongs to Sequence
no?
What am I missing?
Well I found the correct syntax, not sure why the other syntax would not work tho :/
If someone could still explain why the other way does not work, it'd be interesting 🤔
extension Sequence where Element:Equatable {
func intersects<T:Sequence>(with anotherSequence:T) -> Bool
where T.Element == Self.Element {
return self.contains(where: anotherSequence.contains)
}
}