Hi there!

This pitch is about adding `intersect`

along the existing `overlaps`

extension of `ClosedRange`

and `Range`

.

Below a working implementation to extend `ClosedRange`

, analogous to NSIntersectionRange:

```
extension ClosedRange {
public func intersect(_ other: ClosedRange<Bound>) -> ClosedRange<Bound>? {
let lowerBoundMax = max(self.lowerBound, other.lowerBound)
let upperBoundMin = min(self.upperBound, other.upperBound)
let lowerBeforeUpper = lowerBoundMax <= self.upperBound && lowerBoundMax <= other.upperBound
let upperBeforeLower = upperBoundMin >= self.lowerBound && upperBoundMin >= other.lowerBound
if lowerBeforeUpper && upperBeforeLower {
return lowerBoundMax...upperBoundMin
}
return nil
}
}
```

Optionally, a suitable operator could be introduced:

```
precedencegroup SetTheoryPrecendence {
lowerThan: RangeFormationPrecedence
higherThan: ComparisonPrecedence
}
infix operator ∩: SetTheoryPrecendence
extension ClosedRange {
public static func ∩(lhs: ClosedRange, rhs: ClosedRange) -> ClosedRange<Bound>? {
return lhs.intersect(rhs)
}
}
```

For the provided implementation, the following assertions hold true:

```
assert((1.0...2.0) ∩ (2.0...3.0) == 2.0...2.0)
assert((1.0...3.0) ∩ (2.0...4.0) == 2.0...3.0)
assert((1.0...2.0) ∩ (3.0...4.0) == nil)
```

Apart from needing this in one of my projects, I'd say it's a useful tool when working with ranges and would like this to be generally available.

Happy to hear your thoughts!