Skip to content

Instantly share code, notes, and snippets.

@scottmatthewman
Last active August 29, 2015 14:02
Show Gist options
  • Save scottmatthewman/685955ae9402fc033068 to your computer and use it in GitHub Desktop.
Save scottmatthewman/685955ae9402fc033068 to your computer and use it in GitHub Desktop.
A custom 'between' operator for Swift
operator infix >=< {}
@infix func >=< <T: Comparable> (lhs: T, rhs: (T, T)) -> T {
let (min, max) = rhs
var ret = lhs
ret = (ret < min) ? min : ret
ret = (ret > max) ? max : ret
return ret
}
4.5 >=< (0.0, 5.0) // 4.5
-2.5 >=< (0.0, 5.0) // 0.0
10 >=< (1, 8) // 8
/* less useful, but still works */
"XYZ" >=< ("DAL", "LEK") // "LEK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment