Or new AnySequence constructors? So the sequence can be generated more than
once.
let a = AnySequence(from: 1){ $0 * 2 }.prefix(10)
print(Array(a)) // [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
let b = AnySequence(from: 10){ $0 == 0 ? nil : $0 - 1 }
print(Array(b)) // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Code as implemented in Swift 2.2:
extension AnySequence {
init(from: Element, applying: (Element) -> Element) {
self.init({ () -> AnyGenerator<Element> in
var current: Element?
return AnyGenerator{
current = current.map(applying) ?? from
return current
}
})
}
init(from: Element, applying: (Element) -> Element?) {
self.init({ () -> AnyGenerator<Element> in
var current: Element?
return AnyGenerator{
current = current.map(applying) ?? from
return current
}
})
}
}
**Patrick Smith**
> One idea that came out of the core team discussion was something like:
>
> sequence(from: 0) { $0 += 42 }
>
> Since it returns a sequence.
It definitely occurred to me that this was kind of just a way to construct a
generic iterator. Maybe a new AnyIterator (I believe there is such a thing)
constructor?
\--
Brent Royal-Gordon
···
On May 6 2016, at 7:27 am, Brent Royal-Gordon via swift-evolution <swift- evolution@swift.org> wrote:
Sent from my iPhone
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution