<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>On Fri, May 13, 2016, at 11:08 AM, Erica Sadun wrote:<br></div>
<blockquote type="cite"><div>On May 1, 2016, at 5:13 AM, Brent Royal-Gordon via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div>
<div><blockquote type="cite"><div>&nbsp;</div>
<div><div><blockquote type="cite">The proposal has been updated as per feedback from the core team (<a href="https://github.com/apple/swift-evolution/pull/275">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></blockquote><div>&nbsp;</div>
<div>The proposal says this:<br></div>
<div>&nbsp;</div>
<div><span style="white-space:pre;"> </span>public func unfold&lt;T, State&gt;(_ initialState: State, applying: State -&gt; (T, State)?) -&gt; UnfoldSequence&lt;T&gt;<br></div>
<div><span style="white-space:pre;"> </span>public func unfold&lt;T&gt;(_ initialElement: T, apply: T -&gt; T) -&gt; UnfoldSequence&lt;T&gt;<br></div>
<div>&nbsp;</div>
<div>However, the comment implies that the second one should instead be this:<br></div>
<div>&nbsp;</div>
<div><span style="white-space:pre;"> </span>public func unfold&lt;T&gt;(_ initialElement: T, applying: T -&gt; T?) -&gt; UnfoldSequence&lt;T&gt;<br></div>
<div>&nbsp;</div>
<div>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></div>
<div>&nbsp;</div>
<div><span style="white-space:pre;"> </span>public func unfold&lt;T, State&gt;(fromState initialState: State, applying: State -&gt; (T, State)?) -&gt; UnfoldSequence&lt;T&gt;<br></div>
<div><span style="white-space:pre;"> </span>public func unfold&lt;T&gt;(fromFirst initialElement: T, apply: T -&gt; T) -&gt; UnfoldSequence&lt;T&gt;<br></div>
<div>&nbsp;</div>
<div>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></div>
</div>
</div>
</blockquote></div>
<div>&nbsp;</div>
<div>I really don't want to see this discussion die as I have a vested interest in getting this functionality into<br></div>
<div>Swift 3. So let me suggest that<br></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family:Menlo">`sequence(_:, next:) -&gt; AdHocSequence`</span><br></div>
<div>&nbsp;</div>
<div>might be a Swift acceptable solution. &nbsp;We're not going to see fold/unfold pair happen. It's a given that<br></div>
<div>`reduce` is a fixed point in Swift space and `sequence` well describes what this should be doing.<br></div>
<div>&nbsp;</div>
<div>So is it possible to push forward with `sequence`, whose only negative seems to be that it's not as well<br></div>
<div>loved as `unfold`?<br></div>
</blockquote><div>&nbsp;</div>
<div>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></div>
<div>&nbsp;</div>
<div>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>&nbsp;</div>
<div>So given that, I'll suggest the following:<br></div>
<div>&nbsp;</div>
<div>&nbsp; func sequence&lt;T&gt;(initial: T, next: T -&gt; T?) -&gt; UnfoldSequence&lt;T&gt;<br></div>
<div>&nbsp; func sequence&lt;T, State&gt;(state: State, next: (inout State) -&gt; T?) -&gt; UnfoldSequence&lt;T&gt;<br></div>
<div>&nbsp;</div>
<div>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></div>
<div>&nbsp;</div>
<div>-Kevin Ballard</div>
</body>
</html>