I noticed an odd bug in an app and narrowed it down to a surprising cause: if you pass a sequence to Set#intersection, you get different behavior than if you pass a Set, even though both are supported methods of the Set data structure.
You can verify at any Swift REPL:
1> Set([3, 6, 0, 1, 5, 2, 4]).intersection([0, 1, 1, 2, 3, 4, 5])
$R0: Set<Int> = 7 values {
[0] = 5
[1] = 0
[2] = 3
[3] = 1
[4] = 2
[5] = 6
[6] = 4
}
2> Set([3, 6, 0, 1, 5, 2, 4]).intersection(Set([0, 1, 1, 2, 3, 4, 5]))
$R1: Set<Int> = 6 values {
[0] = 1
[1] = 5
[2] = 0
[3] = 4
[4] = 2
[5] = 3
}
The issue here is that the first result has an extra value, the integer 6, even though there is no 6 in the sequence passed to intersection. The second result is correct - the first is wrong. And the only difference between the two calls is that the second call has its sequence converted to a Set.
Am I losing my mind, or is this unexpected behavior?