<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="">Would there be any issue with the return type being AnySequence? It’s used in other areas:</div><div class=""><br class=""></div><div class="">LazySequence &amp; FlattenSequence’s</div><div class="">dropFirst(n:&nbsp;Int) -&gt;&nbsp;AnySequence&lt;Generator.Element&gt;</div><div class="">dropLast(n:&nbsp;Int) -&gt;&nbsp;AnySequence&lt;Generator.Element&gt;</div><div class=""><br class=""></div><div class="">No need to introduce another type, and it’s straight forward to implement with AnySequence.</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 14 May 2016, at 5:07 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="">On Fri, May 13, 2016, at 11:08 AM, Erica Sadun wrote:<br class=""></div>
<blockquote type="cite" class=""><div class="">On May 1, 2016, at 5:13 AM, Brent Royal-Gordon via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div>
<div class=""><blockquote type="cite" class=""><div class="">&nbsp;</div>
<div class=""><div class=""><blockquote type="cite" class="">The proposal has been updated as per feedback from the core team (<a href="https://github.com/apple/swift-evolution/pull/275" class="">https://github.com/apple/swift-evolution/pull/275</a>). This includes removing some last vestiges of Swift 2 naming as well as replacing `iterate(_:apply:)` with an overloaded function `unfold(_:applying:)`.<br class=""></blockquote><div class="">&nbsp;</div>
<div class="">The proposal says this:<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><span style="white-space:pre;" class=""> </span>public func unfold&lt;T, State&gt;(_ initialState: State, applying: State -&gt; (T, State)?) -&gt; UnfoldSequence&lt;T&gt;<br class=""></div>
<div class=""><span style="white-space:pre;" class=""> </span>public func unfold&lt;T&gt;(_ initialElement: T, apply: T -&gt; T) -&gt; UnfoldSequence&lt;T&gt;<br class=""></div>
<div class="">&nbsp;</div>
<div class="">However, the comment implies that the second one should instead be this:<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><span style="white-space:pre;" class=""> </span>public func unfold&lt;T&gt;(_ initialElement: T, applying: T -&gt; T?) -&gt; UnfoldSequence&lt;T&gt;<br class=""></div>
<div class="">&nbsp;</div>
<div class="">I'm not sure I like having these be overloaded on only the return type of the closure. Maybe we could do something like this?<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><span style="white-space:pre;" class=""> </span>public func unfold&lt;T, State&gt;(fromState initialState: State, applying: State -&gt; (T, State)?) -&gt; UnfoldSequence&lt;T&gt;<br class=""></div>
<div class=""><span style="white-space:pre;" class=""> </span>public func unfold&lt;T&gt;(fromFirst initialElement: T, apply: T -&gt; T) -&gt; UnfoldSequence&lt;T&gt;<br class=""></div>
<div class="">&nbsp;</div>
<div class="">That way you're calling either `unfold(fromState:applying:)` or `unfold(fromFirst:applying:)`. (Some further bikeshedding might be needed here—it's late and I'm tired.)<br class=""></div>
</div>
</div>
</blockquote></div>
<div class="">&nbsp;</div>
<div class="">I really don't want to see this discussion die as I have a vested interest in getting this functionality into<br class=""></div>
<div class="">Swift 3. So let me suggest that<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><span class="font" style="font-family:Menlo">`sequence(_:, next:) -&gt; AdHocSequence`</span><br class=""></div>
<div class="">&nbsp;</div>
<div class="">might be a Swift acceptable solution. &nbsp;We're not going to see fold/unfold pair happen. It's a given that<br class=""></div>
<div class="">`reduce` is a fixed point in Swift space and `sequence` well describes what this should be doing.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">So is it possible to push forward with `sequence`, whose only negative seems to be that it's not as well<br class=""></div>
<div class="">loved as `unfold`?<br class=""></div>
</blockquote><div class="">&nbsp;</div>
<div class="">I do like `sequence`, though I'm not sold on the name AdHocSequence (just from that name it's hard to figure out what it does). An alternative is `expand`, which is nice because it pairs with `reduce`, but it's less obvious that it produces a sequence and the name isn't as good with the stateful version.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">As for return type name, we could go ahead and use UnfoldSequence&lt;T&gt; anyway even though the function isn't named `unfold`, because this name will make sense to people who do know what unfold is, and I'm not convinced we can have a meaningful name for people who don't (since SequenceSequence is too silly).</div>
<div class="">&nbsp;</div>
<div class="">So given that, I'll suggest the following:<br class=""></div>
<div class="">&nbsp;</div>
<div class="">&nbsp; func sequence&lt;T&gt;(initial: T, next: T -&gt; T?) -&gt; UnfoldSequence&lt;T&gt;<br class=""></div>
<div class="">&nbsp; func sequence&lt;T, State&gt;(state: State, next: (inout State) -&gt; T?) -&gt; UnfoldSequence&lt;T&gt;<br class=""></div>
<div class="">&nbsp;</div>
<div class="">I'm suggesting `sequence(initial:next:)` instead of the previously-suggested `sequence(from:applying:)` because the term "from" could equally well mean the first element or the state, whereas "initial" should make it more obvious that this value is the first element of the resulting sequence. And I'm using "next" as suggested by Erica because the function does return the next element, and it's similar to the IteratorProtocol method. I've also chosen to change the stateful version to use an inout parameter, as previously suggested, because it's equivalent to the State -&gt; (T, State)? in functionality but is less likely to produce unwanted COW copies.<br class=""></div>
<div class="">&nbsp;</div>
<div class="">-Kevin Ballard</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>