[swift-users] Sampling collections

Milos Rankovic milos at milos-and-slavica.net
Sun Apr 10 20:01:50 CDT 2016


> On 10 Apr 2016, at 23:12, Jens Alfke <jens at mooseyard.com> wrote:
> You can easily implement your own `sample` property. 

The very first email of this thread has a link to my example implementation. Here it is again if you mist it: http://stackoverflow.com/a/30285125/1409907 <http://stackoverflow.com/a/30285125/1409907>… however, my whole point is that I’d prefer if this important feature came with `Foundation`.

> So far Apple hasn’t added Swift-specific functionality to system frameworks…

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. 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.

Please note, though, that my original mail was simply trying to find out if there are people who also wished they had this functionality available upon importing Foundation; if they too would enjoy using it in playgrounds, when teaching, or while testing their code… Imagine opening a blank playground and typing something silly like the following, while fully expecting there to be a `sample` method on arrays, just as there is `componentsSeparatedByString` on strings:

import Foundation

extension String {
	var define: String? {
		return DCSCopyTextDefinition(nil, self, CFRangeMake(0, utf16.count)).map{
			$0.takeRetainedValue() as String
		}
	}
	var trail: String {
		var trail = [self]
		while let word = trail.last?.define?.componentsSeparatedByString(" ").sample {
			trail.append(word)
		}
		return trail.joinWithSeparator(" ") + "!"
	}
}

for i in 1...100 {
	print("Random".trail)
}

milos

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


More information about the swift-users mailing list