[swift-evolution] About the PermutationGenerator
Kevin Ballard
kevin at sb.org
Wed Dec 30 22:34:55 CST 2015
We have plenty of examples of GeneratorTypes that also conform to
SequenceType. There's no harm in it, and it saves having to declare a
separate type that consists solely of a generate() method that returns
the generator. In fact, the stdlib automatically derives the generate()
method for any GeneratorType that conforms to SequenceType already,
specifically to make this pattern as easy as possible. The only thing at
all odd about this is the name PermutationGenerator doesn't tell you
it's a sequence, but that's no different than AnyGenerator,
EmptyGenerator, EnumerateGenerator, FlattenGenerator, IndexingGenerator,
JoinGenerator, LazyFilterGenerator, LazyMapGenerator, RangeGenerator,
and UnsafeBufferPointerGenerator (though to be fair all of those have a
matching distinct Sequence type).
As for PermutationCollection, that's not a bad idea. I guess the biggest
objection is that I'm not sure if PermutationGenerator is even pulling
its own weight, and adding more permutation types won't help. I don't
see any uses of PermutationGenerator in the stdlib, and I've never found
a use for it in my own code, though I imagine that someone somewhere is
actually using it. In any case, the argument "because we can" isn't
sufficient to add something to the stdlib; it has to actually be of
enough use to be worth both the added maintenance burden and the added
code size.
That said, even if we do add a PermutationGenerator, I definitely don't
think extending CollectionType with a collect() method set like that is
worth doing. If we did have a method it should probably be called
"permute", but I doubt enough people would use it to be worth the added
semantic overhead of yet another collection method.
-Kevin Ballard
On Wed, Dec 30, 2015, at 06:22 PM, Susan Cheng via swift-evolution wrote:
> 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:
>
> publicextensionCollectionType {
>
> @warn_unused_result
> func collect<I : SequenceTypewhereIndex ==
> 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)
> }
> }
>
> publicextensionLazyCollectionType {
>
> @warn_unused_result
> func collect<I : SequenceTypewhereElements.Index ==
> I.Generator.Element>(indices: I) ->
> LazySequence<PermutationGenerator<Elements, I>> {
> return self.elements.collect(indices).lazy
> }
>
> @warn_unused_result
> func collect<I : CollectionTypewhereElements.Index ==
> I.Generator.Element>(indices: I) ->
> LazyCollection<PermutationCollection<Elements, I>> {
> return self.elements.collect(indices).lazy
> }
>
> }
>
>
> _________________________________________________
> swift-evolution mailing list swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151230/784d0cf0/attachment.html>
More information about the swift-evolution
mailing list