Skip to content

Instantly share code, notes, and snippets.

@nubbel
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nubbel/2fd15fd358110b6d6fc5 to your computer and use it in GitHub Desktop.
Save nubbel/2fd15fd358110b6d6fc5 to your computer and use it in GitHub Desktop.
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