Created
January 19, 2015 17:56
-
-
Save MarkQSchultz/afbfac3c774179e38ebb to your computer and use it in GitHub Desktop.
Assignment operators that max or min a value against a variable
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
infix operator >>>= { | |
associativity right | |
precedence 90 | |
assignment | |
} | |
infix operator <<<= { | |
associativity right | |
precedence 90 | |
assignment | |
} | |
func >>>= <T: Comparable> (inout left: T, right: T) { | |
left = max(left, right) | |
} | |
func <<<= <T: Comparable> (inout left: T, right: T) { | |
left = min(left, right) | |
} | |
// usage | |
var x = 5 | |
x >>>= 6 // equivalent to x = max(x, 6). Will set x to 6. | |
x >>>= 3 // equivalent to x = max(x, 3). Will set x to 6. | |
x <<<= 4 // equivalent to x = min(x, 4). Will set x to 4. | |
x <<<= 10 // equivalent to x = min(x, 10). Will set x to 4. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment