<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">This sounds fair to me. I imagine a functional version would return two item tuple instead of mutating, so would it be that similar to what people expect?</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 20 May 2016, at 10:52 AM, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">


<title class=""></title>

<div class=""><div class="">After having given this some thought, it seems apparent that `sequence(state:next:)` is  equivalent to `AnyIterator({ ... })` where the closure captures a single mutable variable. The microbenchmark performance may differ slightly, as the AnyIterator version will allocate a box on the heap to hold the captured variable (assuming it can't get inlined entirely), whereas UnfoldSequence won't. But the functionality is the same.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Thus the question: do we want to keep `sequence(state:next:)` or is it too close to AnyIterator in functionality? Arguments in favor of `sequence(state:next:)`:<br class=""></div>
<div class="">&nbsp;</div>
<div class="">* It's equivalent to unfold and the dual of reduce, so people who've used functional programming languages may expect it to exist.<br class=""></div>
<div class="">* It allows you to create ad-hoc stateful sequences without polluting the current scope with a variable that exists solely to be captured.<br class=""></div>
<div class="">* If the cost of a small heap allocation is significant for your code, it may be more performant than AnyIterator.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Personally, the most important reason here for me is not having to pollute the current closure with a variable. And this could actually be solved another way, by allowing the use of `var` in a capture list, which would let you say something like `AnyGenerator({ [var state=foo] in ... })`.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">Given all this, at this point I'm actually leaning towards saying`sequence(state:next:)` doesn't pull its own weight and we should just go with `sequence(initial:next:)`.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">-Kevin Ballard</div>
<div class="">&nbsp;</div>
<div class="">On Thu, May 19, 2016, at 05:37 PM, Trent Nadeau via swift-evolution wrote:<br class=""></div>
<blockquote type="cite" class=""><div dir="ltr" class="">Ah, yes. I apologize. The fact that state is inout, and the same instance is always passed in confused me. Thanks for the correction.<br class=""></div>
<div class=""><div class="">&nbsp;</div>
<div defang_data-gmailquote="yes" class=""><div class="">On Thu, May 19, 2016 at 7:46 PM, Brent Royal-Gordon <span dir="ltr" class="">&lt;<a href="mailto:brent@architechies.com" class="">brent@architechies.com</a>&gt;</span> wrote:<br class=""></div>
<blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;padding-left:1ex;" defang_data-gmailquote="yes" class=""><div class=""><span class="">&gt; Also note that there's a typo in the second example:<br class=""> &gt;<br class=""> &gt; for view in sequence(initial: someView, next: { $0.<br class=""> &gt; superview }) {<br class=""> &gt;<br class=""> &gt; // someView, someView.superview, someView.superview.superview, ...<br class=""> &gt;<br class=""> &gt; }<br class=""> &gt;<br class=""> &gt;<br class=""> &gt; should be:<br class=""> &gt;<br class=""> &gt; for view in sequence(state: someView, next: { $0.<br class=""> &gt; superview }) {<br class=""> &gt;<br class=""> &gt; // someView, someView.superview, someView.superview.superview, ...<br class=""> &gt;<br class=""> &gt; }<br class=""> <br class=""> </span>I don't think these are mistakes—in each iteration of the loop, $0 is supposed to be the view from the previous iteration.</div>
<div class="">&nbsp;</div>
<div class=""> If you wanted an example using `state`, here's one which is roughly equivalent to `stride(from: 1.0, to: 2.0, by: 0.1)`, using a non-error-accumulating algorithm:<br class=""></div>
<div class="">&nbsp;</div>
<div class=""> let start = 1.0<br class=""></div>
<div class=""> let end = 2.0<br class=""></div>
<div class=""> let distance = 0.1<br class=""></div>
<div class="">&nbsp;</div>
<div class=""> for color in sequence(state: -1.0, next: { $0 += 1; let next = start + $0 * distance; return next &lt; end ? next : nil }) {<br class=""></div>
<div class=""> …<br class=""></div>
<div class=""> }<br class=""></div>
<div class=""> <span class=""><span class="colour" style="color:rgb(136, 136, 136)"><br class=""> --<br class=""> Brent Royal-Gordon<br class=""> Architechies<br class=""> </span></span></div>
</blockquote></div>
<div class="">&nbsp;</div>
<div class="">&nbsp;</div>
<div class="">&nbsp;</div>
<div class="">-- <br class=""></div>
<div class="">Trent Nadeau<br class=""></div>
</div>
<div class=""><u class="">_______________________________________________</u><br class=""></div>
<div class="">swift-evolution mailing list<br class=""></div>
<div class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""></div>
<div class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div>
</blockquote><div class="">&nbsp;</div>
</div>

_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>