<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>On Thu, May 19, 2016, at 05:59 PM, Patrick Smith wrote:<br></div>
<blockquote type="cite"><div>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?<br></div>
</blockquote><div> </div>
<div>A functional version of `sequence(state:next:)` would indeed use the type signature of `next` as `State -> (T, State)?`. This is precisely how Haskell's `unfoldr` works. But doing that with Swift where the state contains Copy-on-Write data structures will end up with unnecessary copies if the COW data structure is mutated.<br></div>
<div> </div>
<div>-Kevin</div>
<div> </div>
<blockquote type="cite"><div><blockquote type="cite"><div>On 20 May 2016, at 10:52 AM, Kevin Ballard via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div>
<div> </div>
<div><div><div>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></div>
<div> <br></div>
<div>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></div>
<div> <br></div>
<div>* It's equivalent to unfold and the dual of reduce, so people who've used functional programming languages may expect it to exist.<br></div>
<div>* It allows you to create ad-hoc stateful sequences without polluting the current scope with a variable that exists solely to be captured.<br></div>
<div>* If the cost of a small heap allocation is significant for your code, it may be more performant than AnyIterator.<br></div>
<div> <br></div>
<div>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></div>
<div> <br></div>
<div>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></div>
<div> <br></div>
<div>-Kevin Ballard<br></div>
<div> <br></div>
<div>On Thu, May 19, 2016, at 05:37 PM, Trent Nadeau via swift-evolution wrote:<br></div>
<blockquote type="cite"><div dir="ltr">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></div>
<div><div> <br></div>
<div><div>On Thu, May 19, 2016 at 7:46 PM, Brent Royal-Gordon <span dir="ltr"><<a href="mailto:brent@architechies.com">brent@architechies.com</a>></span> wrote:<br></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;"><div><span>> Also note that there's a typo in the second example:<br> ><br> > for view in sequence(initial: someView, next: { $0.<br> > superview }) {<br> ><br> > // someView, someView.superview, someView.superview.superview, ...<br> ><br> > }<br> ><br> ><br> > should be:<br> ><br> > for view in sequence(state: someView, next: { $0.<br> > superview }) {<br> ><br> > // someView, someView.superview, someView.superview.superview, ...<br> ><br> > }<br> <br> </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> <br></div>
<div>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></div>
<div> <br></div>
<div>let start = 1.0<br></div>
<div>let end = 2.0<br></div>
<div>let distance = 0.1<br></div>
<div> <br></div>
<div>for color in sequence(state: -1.0, next: { $0 += 1; let next = start + $0 * distance; return next < end ? next : nil }) {<br></div>
<div>…<br></div>
<div>}<br></div>
<div><span><span class="colour" style="color:rgb(136, 136, 136)"><br>--<br> Brent Royal-Gordon<br> Architechies</span></span></div>
</blockquote></div>
<div> <br></div>
<div> <br></div>
<div> <br></div>
<div>-- <br></div>
<div>Trent Nadeau<br></div>
</div>
<div><u>_______________________________________________</u><br></div>
<div>swift-evolution mailing list<br></div>
<div><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div>
</blockquote><div> <br></div>
</div>
<div>_______________________________________________<br></div>
<div>swift-evolution mailing list<br></div>
<div><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div>https://lists.swift.org/mailman/listinfo/swift-evolution<br></div>
</div>
</blockquote></div>
</blockquote><div> </div>
</body>
</html>