[swift-evolution] About the PermutationGenerator

Susan Cheng susan.doggie at gmail.com
Wed Dec 30 20:22:47 CST 2015


PermutationGenerator confuses me that it's confirm to both of SequenceType
and GeneratorType. Should it replace by PermutationSequence and
PermutationGenerator?


Also, we should have a PermutationCollection because we can:


public struct PermutationCollection<C : CollectionType, I : CollectionType
where C.Index == I.Generator.Element> : CollectionType {



    public typealias Generator = PermutationGenerator<C, I>



    public typealias Index = I.Index

    public typealias Element = C.Generator.Element



    private let _base: C

    private let _indices: I



    public subscript(idx: Index) -> Element {

        return _base[_indices[idx]]

    }



    public var startIndex : Index {

        return _indices.startIndex

    }

    public var endIndex : Index {

        return _indices.endIndex

    }



    public var count : Index.Distance {

        return _indices.count

    }



    public func generate() -> Generator {

        return PermutationGenerator(elements: _base, indices: _indices)

    }

}


and some methods provide:


public extension CollectionType {



    @warn_unused_result

    func collect<I : SequenceType where Index == I.Generator.Element>(indices:
I) -> PermutationGenerator<Self, I> {

        return PermutationGenerator(elements: self, indices: indices)

    }



    @warn_unused_result

    func collect<I : CollectionType where Index ==
I.Generator.Element>(indices:
I) -> PermutationCollection<Self, I> {

        return PermutationCollection(_base: self, _indices: indices)

    }

}


public extension LazyCollectionType {



    @warn_unused_result

    func collect<I : SequenceType where Elements.Index == I.Generator.
Element>(indices: I) -> LazySequence<PermutationGenerator<Elements, I>> {

        return self.elements.collect(indices).lazy

    }



    @warn_unused_result

    func collect<I : CollectionType where Elements.Index == I.Generator.
Element>(indices: I) -> LazyCollection<PermutationCollection<Elements, I>> {

        return self.elements.collect(indices).lazy

    }

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151231/058c4015/attachment.html>


More information about the swift-evolution mailing list