[swift-users] Sampling collections

Milos Rankovic milos at milos-and-slavica.net
Sun Apr 10 16:40:34 CDT 2016


> On 10 Apr 2016, at 22:16, Erica Sadun <erica at ericasadun.com> wrote:
> 
> I don't think general random sources are a good fit for core functionality

I’m sorry, Erica, I still do not understand how your comments about “core functionality” reflect on my original question – have you seen it?

Certainly, there is plenty of precedent where Foundation extends Standard Library types and protocols:

// Foundation

extension String {
    public func enumerateLinguisticTagsInRange…
}

// CoreGraphics

extension Double {
    public init(_ value: CGFloat)
}

// Darwin

func yn(n: Int, _ x: Double) -> Double //...which are the bessel functions of first and second kind!

What I’m talking about would not look out of place with linguistic tags and bassel functions:

// Foundation

extension UnsignedIntegerType {
    static var random: Self
}

extension ClosedInterval where Bound : UnsignedIntegerType {
    var random: Bound
}

extension ClosedInterval where Bound : SignedIntegerType {
    var random: Bound
}

extension CollectionType where Index.Distance == Int {
    var sample: Generator.Element?
}

… which we could use by:

    import Foundation
    
    (1..<4).sample
    [1,2,3].sample
    "abc".characters.sample
    ["a": 1, "b": 2, "c": 3].sample

milos

> On 10 Apr 2016, at 22:16, Erica Sadun <erica at ericasadun.com> wrote:
> 
> 
>> On Apr 10, 2016, at 2:39 PM, Milos Rankovic <milos at milos-and-slavica.net <mailto:milos at milos-and-slavica.net>> wrote:
>> 
>>> On 10 Apr 2016, at 21:23, Erica Sadun <erica at ericasadun.com <mailto:erica at ericasadun.com>> wrote:
>>> 
>>> I do not think it's the role of a core language to worry about things like distributions, bias, and sampling.
>> 
>> Why do you mention “the role of a core language” here? That was explicitly not the ambition of my question. I’m talking about extending the Standard Library types and protocols in the Foundation framework (as this is already done on a large scale). Or, if this is what you mean by “core language”, how does capitalising strings according to the rules of grammar of every language on the planet qualify as any more fitting the domain of the core language?
>> 
>> milos
>> 
> 
> While I don't think general random sources are a good fit for core functionality, apparently, NSRandomSpecifier exists: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSRandomSpecifier_Class/index.html#//apple_ref/occ/cl/NSRandomSpecifier <https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSRandomSpecifier_Class/index.html#//apple_ref/occ/cl/NSRandomSpecifier>
> 
> Other material I consulted:
> Standard Library: https://en.wikipedia.org/wiki/Standard_library <https://en.wikipedia.org/wiki/Standard_library>
> Foundation: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/ObjC_classic/ <https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/ObjC_classic/> 
> GameplayKit Randomization: https://developer.apple.com/library/ios/documentation/General/Conceptual/GameplayKit_Guide/RandomSources.html#//apple_ref/doc/uid/TP40015172-CH9-SW1 <https://developer.apple.com/library/ios/documentation/General/Conceptual/GameplayKit_Guide/RandomSources.html#//apple_ref/doc/uid/TP40015172-CH9-SW1>
> 
> -- E
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160410/ea4a0fad/attachment.html>


More information about the swift-users mailing list