[swift-evolution] [Accepted with modifications] SE-0045: Add scan, prefix(while:), drop(while:), and unfold to the stdlib
Patrick Smith
pgwsmith at gmail.com
Thu May 5 22:33:08 CDT 2016
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**
On May 6 2016, at 7:27 am, Brent Royal-Gordon via swift-evolution <swift-
evolution at swift.org> wrote:
> > 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
Sent from my iPhone
_______________________________________________
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/20160505/2685abc5/attachment.html>
More information about the swift-evolution
mailing list