Create a gist now

Instantly share code, notes, and snippets.

struct MySet<KeyType : Hashable> : Sequence, ArrayLiteralConvertible
{
var dictionaryOfItems = Dictionary<KeyType,Bool>()
typealias GeneratorType = MapSequenceGenerator<Dictionary<KeyType, Bool>.GeneratorType, KeyType>
init() {}
init(array: KeyType[]) {
for item in array {
self.dictionaryOfItems.updateValue(true, forKey: item)
}
}
mutating
func addObject(item:KeyType) -> Void {
self.dictionaryOfItems.updateValue(true, forKey: item)
}
func containsObject(item:KeyType) -> Bool {
return self.dictionaryOfItems[item].getLogicValue()
}
func generate() -> GeneratorType {
return dictionaryOfItems.keys.generate()
}
static func convertFromArrayLiteral (elements: KeyType...) -> MySet<KeyType> {
return MySet (array: elements)
}
}
// the ArrayLiteralConvertible lets you do
let set : MySet<Int> = [1,2,3]
@algal
algal commented Jun 11, 2014

Cool. A clear improvement. I don't understand the reason for "Map" in MapSequenceGenerator, or how it relates to the DictionaryGenerator.

A little worrying how reifying all these abstractions into types is quickly producing Java-esque names for things, like three car pile-ups of abstract nouns, instead of the sanity of Cocoa. We'll see how it goes..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment