Last active
August 29, 2015 14:02
-
-
Save scottmatthewman/685955ae9402fc033068 to your computer and use it in GitHub Desktop.
A custom 'between' operator for Swift
This file contains 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
operator infix >=< {} | |
@infix func >=< <T: Comparable> (lhs: T, rhs: (T, T)) -> T { | |
let (min, max) = rhs | |
var ret = lhs | |
ret = (ret < min) ? min : ret | |
ret = (ret > max) ? max : ret | |
return ret | |
} |
This file contains 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
4.5 >=< (0.0, 5.0) // 4.5 | |
-2.5 >=< (0.0, 5.0) // 0.0 | |
10 >=< (1, 8) // 8 | |
/* less useful, but still works */ | |
"XYZ" >=< ("DAL", "LEK") // "LEK" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment