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

Nicholas Maccharoli nmaccharoli at gmail.com
Fri Mar 10 03:27:27 CST 2017


​Dave,
​

> I’m ok with doing it as an extension on `Comparable`, although we should
> add an overload for regular ranges, too.


​Yeah, I completely agree and for now I'll drop the topic of removing the
global definitions for min / max​.

So the aim of this proposal would be to add `clamp` to both `Range` and
`Comparable`.

- Nick


On Fri, Mar 10, 2017 at 6:16 PM, David Sweeris <davesweeris at mac.com> wrote:

>
> 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/101214a8/attachment.html>


More information about the swift-evolution mailing list