[swift-evolution] Add a `clamp` function to Algorithm.swift

Nevin Brackett-Rozinsky nevin.brackettrozinsky at gmail.com
Thu Mar 9 22:41:44 CST 2017


Iā€™d be on board with an extension of Comparable so you could write
ā€œ16.clamped(to: 0...10)ā€. Something along the lines of:

extension Comparable {
    func clamped(to range: ClosedRange<Self>) -> Self {
        return max(range.lowerBound, min(self, range.upperBound))
    }
}

Nevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170309/99a53328/attachment.html>


More information about the swift-evolution mailing list