Last active
August 29, 2015 14:04
-
-
Save nubbel/2fd15fd358110b6d6fc5 to your computer and use it in GitHub Desktop.
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 left } | |
// left-most comparison | |
func <<T : Comparable>(lhs: T, rhs: T) -> ((T, T) -> Bool, () -> T) -> Bool { | |
return { (op: (T, T) -> Bool, value: () -> T) in | |
(lhs < rhs) && op(rhs, value()) | |
} | |
} | |
// mid comparisons | |
func <<T : Comparable>(lhs: ((T, T) -> Bool, () -> T) -> Bool, rhs: @autoclosure () -> T) -> ((T, T) -> Bool, () -> T) -> Bool { | |
return { (op: (T, T) -> Bool, value: () -> T) in | |
lhs({$0 < $1}, rhs) && op(rhs(), value()) | |
} | |
} | |
// right-most comparison | |
func <<T : Comparable>(lhs: ((T, T) -> Bool, () -> T) -> Bool, rhs: @autoclosure () -> T) -> Bool { | |
return lhs({$0 < $1}, rhs) | |
} | |
// Example: | |
var x = 3 | |
var y = 5 | |
var z = 7 | |
if 1 < x < y < z < 10 { | |
true | |
} | |
else { | |
false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment