Created
October 24, 2015 11:19
-
-
Save mzaks/85c98d7411dc50428ad4 to your computer and use it in GitHub Desktop.
Boundary implementation through extending Comparable protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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