Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. /// Determine whether a value is within an inclusive range
  2. func isValue <T : Comparable>(value: T, inClosedInterval low: T, high: T) -> Bool {
  3. return low <= value && value <= high
  4. }
  5.  
  6. /// Determine whether a value is within a half-open range (include bottom but not top)
  7. func isValue <T : Comparable>(value: T, inHalfOpenInterval low: T, high: T) -> Bool {
  8. return low <= value && value < high
  9. }
  10.  
  11. /// Determine whether a value is within an exclusive range
  12. func isValue <T : Comparable>(value: T, inOpenInterval low: T, high: T) -> Bool {
  13. return low < value && value < high
  14. }
  15.  
  16.  
  17. // Examples
  18.  
  19. isValue(10, inClosedInterval: 0, 10) // true
  20. isValue(10, inHalfOpenInterval: 0, 10) // false
  21.  
  22. isValue(0.0001, inOpenInterval: 0.0, 1.0) // true
  23. isValue(0.9999, inOpenInterval: 0.0, 1.0) // true
  24. isValue(0.0, inOpenInterval: 0.0, 1.0) // false
  25. isValue(1.0, inOpenInterval: 0.0, 1.0) // false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement