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

David Sweeris davesweeris at mac.com
Fri Mar 10 03:16:42 CST 2017


> On Mar 10, 2017, at 1:12 AM, Nicholas Maccharoli via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Sorry for sidetracking by talking about dumping the global definitions of `min` and `max` but if that could be done and it were decided by the swift community that adding a clamp function would be appropriate, I guess with the array implementations of min / max the clamp function might be implemented like this?
> 
> extension Comparable {
> 
>     func clamped(to range: ClosedRange<Self>) -> Self {
> 
>         return [range.lowerBound, [self, range.upperBound].min()!].max()!
> 
>     }
> 
> }
> 
I’m ok with doing it as an extension on `Comparable`, although we should add an overload for regular ranges, too.

- Dave Sweeris 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170310/57d20caa/attachment.html>


More information about the swift-evolution mailing list