Skip to content

Instantly share code, notes, and snippets.

@mzaks
Created October 24, 2015 11:19
Show Gist options
  • Select an option

  • Save mzaks/85c98d7411dc50428ad4 to your computer and use it in GitHub Desktop.

Select an option

Save mzaks/85c98d7411dc50428ad4 to your computer and use it in GitHub Desktop.
Boundary implementation through extending Comparable protocol
extension Comparable {
func insideBoundary(a : Self, _ b : Self) -> Bool {
let (left, right) = (min(a,b), max(a, b))
return self >= left && self <= right
}
func insideBoundary(inclusive a : Self, exclusive b : Self) -> Bool {
return insideBoundary(a, b) && self != b
}
func insideBoundary(exclusive a : Self, exclusive b : Self) -> Bool {
return insideBoundary(inclusive: a, exclusive: b) && self != a
}
}
10.insideBoundary(10, 50)
10.insideBoundary(inclusive: 10, exclusive: 50)
10.insideBoundary(inclusive: 50, exclusive: 10)
10.insideBoundary(exclusive: 10, exclusive: 50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment