[swift-evolution] [Idea] Add `bounds` function to standard library

Dominik Pich dominik at pich.info
Thu Sep 1 01:19:11 CDT 2016

I agree with karl. there is nothing really mathematical with min/max

e.g. find the longest sequence of characters in a string or the smallest array or the minimal x coordinate of view objects…
min/max/clamp are needed everywhere.


Web: https://pich.info
Twitter: @DaijDjan
Facebook: Dominik.Pich

> On Aug 31, 2016, at 3:53 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
> Comparable makes semantic guarantees about how values of conforming types might be ordered. You don't need `min` or `max` for that to be useful, since it's trivial to implement using comparison operators.
> Basic numeric types require compiler magic and thus belong in the standard library. Likewise, dictionaries have special syntactic sugar and have uses for types that can guarantee comparable semantics. A decimal type, though, can be implemented outside the standard library and probably would belong in a math library. Likewise mathematical constants such as e. I think min and max fall into the latter category.
> On Wed, Aug 31, 2016 at 8:10 AM Karl <razielim at gmail.com <mailto:razielim at gmail.com>> wrote:
> > On 30 Aug 2016, at 10:18, Xiaodi Wu via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> >
> > As an additive proposal, I don't think this would be in scope for the current phase of Swift 4.
> >
> > Looking forward, though, I'm not sure this belongs in the standard library. In general, my understanding is that Swift's standard library is deliberately small, and that the criteria for additions are that it's widely used *and* also non-trivial for the user to write correctly. I've had to use clamping, obviously, but it's a trivial one-liner that is hard to write incorrectly. If anything, I'd be in favor of removing max and min into a future math library outside the standard library.
> min & max (and clamping) are hardly “math” operations. They operate on Comparables, so you can apply them to more abstract things than just numbers.
> Otherwise, you might as well put Comparable and all standard numeric types like Int and Float in a math library, too.
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160901/2ac5b004/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160901/2ac5b004/attachment.sig>

More information about the swift-evolution mailing list