Skip to content

Instantly share code, notes, and snippets.

@KaiCode2
Last active September 2, 2015 20:36
Show Gist options
  • Save KaiCode2/130b7f689e988901f59e to your computer and use it in GitHub Desktop.
Save KaiCode2/130b7f689e988901f59e to your computer and use it in GitHub Desktop.
import Foundation
enum Position {
case Over
case Under
}
extension Comparable {
func isBetween<T where T: Comparable>(lhs: T, _ rhs: T) -> (Bool, Position?) {
assert(self is T, "\(self) of type \(self.dynamicType), is not comparable to the provided parameters of type \(T.self).")
if let someSelf = self as? T {
if someSelf >= lhs && someSelf <= rhs {
return (true, nil)
} else if someSelf < lhs {
return (false, Position.Under)
} else if someSelf > rhs {
return (false, Position.Over)
}
}
return (false, nil)
}
}
//MARK: Example
//
//
//let number = 7
//
//switch number.isBetween(1, 10) {
//case (true, nil):
// print("between")
//case (false, .Some(.Over)):
// print("over")
//case (false, .Some(.Under)):
// print("under")
//default:
// print("other")
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment