<head></head><body>Or new AnySequence constructors? So the sequence can be generated more than once.<div><div><br></div><div>let a = AnySequence(from: 1){ $0 * 2 }.prefix(10)<br>print(Array(a)) // [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]<br><br>let b = AnySequence(from: 10){ $0 == 0 ? nil : $0 - 1 }<br>print(Array(b)) // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]<br></div><div><br></div><div><br></div><div>Code as implemented in Swift 2.2:</div><div><div><br></div><div>extension AnySequence {<br> init(from: Element, applying: (Element) -> Element) {<br> self.init({ () -> AnyGenerator<Element> in<br> var current: Element?<br> return AnyGenerator{<br> current = current.map(applying) ?? from<br> return current<br> }<br> })<br> }<br> <br> init(from: Element, applying: (Element) -> Element?) {<br> self.init({ () -> AnyGenerator<Element> in<br> var current: Element?<br> return AnyGenerator{<br> current = current.map(applying) ?? from<br> return current<br> }<br> })<br> }<br>}<br></div><div><br></div><br><!-- <signature> --><b>Patrick Smith</b><br><!-- </signature> --></div></div><div class="gmail_quote">
On May 6 2016, at 7:27 am, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org> wrote:
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<p>> One idea that came out of the core team discussion was something like:<br>> <br>> sequence(from: 0) { $0 += 42 }<br>> <br>> Since it returns a sequence.</p>
<p>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?</p>
<p>-- <br>Brent Royal-Gordon<br>Sent from my iPhone<br>_______________________________________________<br>swift-evolution mailing list<br>swift-evolution@swift.org<br>https://lists.swift.org/mailman/listinfo/swift-evolution</p>
</blockquote>
</div></body>