[swift-users] Sampling collections
jens at mooseyard.com
Sun Apr 10 20:17:33 CDT 2016
> On Apr 10, 2016, at 6:01 PM, Milos Rankovic <milos at milos-and-slavica.net> wrote:
> As I already stressed, I certainly do not imagine this to be “Swift-specific”, nor do I see any reason it would need to be.
It sounds like you’re asking for a `sample` property to be added to NSArray, NSDictionary, NSSet, etc. You could certainly request that Apple do that, by filing a request at http://bugreport.apple.com, but don’t expect a reply; Apple’s framework teams are notoriously opaque.
Putting on my framework-designer hat, I’d argue that “random” is a broad concept with several possible implementations. Which RNG does `sample` use? Pick a cryptographic one and it might be too slow for some use cases; pick a fast one and it'd be insufficiently random, making it dangerous to use for anything related to security. The right answer might be to have a RNG protocol, with several implementations backed by different generators, that exposes a method like `randomElement(Collection)`.
> Also, it looks like you’ve missed my previous email in this thread where I give examples of current Foundation extensions of Standard Library types, and where I also make the bare bones of my wish-list rather more explicit.
The way those are implemented is a weird hack, so they’re not actually good examples of what you’re asking for.
It’s not that Apple's Foundation framework contains any extensions to Swift; Foundation is lower-level and I don’t believe it has any knowledge at all of Swift. So this is not the same effect as when you import a Swift library to get extensions. Rather, importing Foundation is a hardwired signal to the Swift compiler to activate the implicit bridging between Swift’s String class and Foundation’s NSString (and likewise for Array/NSArray, etc.)
In Swift 3 this will supposedly change: these APIs will be added directly to the Swift classes, removing the need for bridging.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-users