Last active
September 2, 2015 20:36
-
-
Save KaiCode2/130b7f689e988901f59e to your computer and use it in GitHub Desktop.
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
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