Created
June 17, 2015 12:23
-
-
Save oisdk/243be25cfe9fdc57634a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct UniquesGen<G : GeneratorType where G.Element : Hashable> : GeneratorType { | |
typealias Element = G.Element | |
private var prevs: Set<Element> | |
private var g: G | |
public mutating func next() -> Element? { | |
return g.next().flatMap{n in prevs.contains(n) ? next() : {prevs.insert(n); return n}()} | |
} | |
} | |
public struct UniquesSeq<S : SequenceType where S.Generator.Element : Hashable> : SequenceType { | |
typealias Generator = UniquesGen<S.Generator> | |
private let seq: S | |
public func generate() -> Generator { | |
return UniquesGen(prevs: [], g: seq.generate()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment