[swift-evolution] About the PermutationGenerator

Susan Cheng susan.doggie at gmail.com
Thu Dec 31 21:46:51 CST 2015


How GeneratorType confirm to Equatable??

struct Fib : SequenceType {

    var a: Int
    var b: Int

    var limit: Int

    func generate() -> FibGenerator {
        return Generator(a: a, b: b, limit: limit)
    }
}

let c = Multipass(Fib(a: 1, b: -1, limit: 10))

-Susan


2016-01-01 11:17 GMT+08:00 Dave Abrahams <dabrahams at apple.com>:

> FWIW, Indexable is an implementation artifact that will go away when
> Swift’s generics system is improved.
>
> But if your real objection is that you have to come up with an Index and a
> subscripting operator, I can understand that.  Part of the reason for this
> is our reluctance to create any distinct protocols with identical syntactic
> requirements <
> http://news.gmane.org/find-root.php?message_id=2A3E0C76-1C88-4752-8A70-AA64BB14223A@apple.com>.
> To justify having a separate multi-pass sequence protocol, there would have
> to be a significant/important class of multi-pass sequences for which
> CollectionType was unimplementable without serious costs.
>
> In principle there’s a way to ease the pain of creating CollectionType
> conformances for multipass SequenceTypes…if only it didn’t crash the
> compiler <https://bugs.swift.org/browse/SR-427> ;-).  Here’s a variation
> that uses a generic adapter instead of a protocol conformance declaration:
>
> /// A `CollectionType` containing the same elements as `Base`, without
> storing them.
> ///
> /// - Requires: `Base` supports multiple passes (traversing it does not
> ///   consume the sequence), and `Base.Generator` has value semantics
> public struct Multipass<Base: SequenceType where Base.Generator:
> Equatable> : CollectionType {
>   public var startIndex: MultipassIndex<Base> {
>     var g = _base.generate()
>     return MultipassIndex(buffer: g.next(), generator: g)
>   }
>
>   public var endIndex: MultipassIndex<Base> {
>     return MultipassIndex(buffer: nil, generator: _base.generate())
>   }
>
>   public subscript(position: MultipassIndex<Base>) ->
> Base.Generator.Element {
>     return position.buffer!
>   }
>
>   public init(_ base: Base) {
>     _base = base
>   }
>
>   var _base: Base
> }
>
> // Note: Requires T.Generator has value semantics
> public struct MultipassIndex<T: SequenceType where T.Generator: Equatable>
> : ForwardIndexType {
>   public func successor() -> MultipassIndex {
>     var r = self
>     r.buffer = r.generator.next()
>     return r
>   }
>   var buffer: T.Generator.Element?
>   var generator: T.Generator
> }
>
> public func == <T>(x: MultipassIndex<T>, y: MultipassIndex<T>) -> Bool {
>   return x.buffer == nil && y.buffer == nil || x.generator == y.generator
> }
>
> //===--- An example fibonacci sequence
> ------------------------------------===//
> struct FibGenerator : GeneratorType {
>   mutating func next() -> Int? {
>     let c = a + b
>     a = b
>     b = c
>     return a < limit ? a : nil
>   }
>   var a, b, limit: Int
> }
>
>
> struct Fib : SequenceType {
>   var limit = 1000
>
>   func generate() -> FibGenerator {
>     return Generator(a: 0, b: 1, limit: limit)
>   }
> }
>
> //===--- Adapt Fib for use with Multipass
> ---------------------------------===//
> extension FibGenerator : Equatable {}
> func == (x: Fib.Generator, y: Fib.Generator) -> Bool {
>   return x.a == y.a
> }
>
> //===--- Demonstration
> ----------------------------------------------------===//
> let c = Multipass(Fib())
> print(c.first)
> print(c.count)
> print(c.lazy.map { $0 + 1 })
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160101/492180d1/attachment.html>


More information about the swift-evolution mailing list