<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>AnySequence is specifically used to erase the type of an underlying sequence, and I'm guessing that using it here will make it harder to optimize loops that use this.</div>
<div> </div>
<div>-Kevin Ballard</div>
<div> </div>
<div>On Fri, May 13, 2016, at 10:20 PM, Patrick Smith wrote:<br></div>
<blockquote type="cite"><div>Would there be any issue with the return type being AnySequence? It’s used in other areas:<br></div>
<div> </div>
<div>LazySequence & FlattenSequence’s<br></div>
<div>dropFirst(n: Int) -> AnySequence<Generator.Element><br></div>
<div>dropLast(n: Int) -> AnySequence<Generator.Element><br></div>
<div> </div>
<div>No need to introduce another type, and it’s straight forward to implement with AnySequence.<br></div>
<div> </div>
<div> </div>
<div><blockquote type="cite"><div>On 14 May 2016, at 5:07 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>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 <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div>
<div><blockquote type="cite"><div> <br></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> <br></div>
<div>The proposal says this:<br></div>
<div> <br></div>
<div><span style="white-space:pre;"></span>public func unfold<T, State>(_ initialState: State, applying: State -> (T, State)?) -> UnfoldSequence<T><br></div>
<div><span style="white-space:pre;"></span>public func unfold<T>(_ initialElement: T, apply: T -> T) -> UnfoldSequence<T><br></div>
<div> <br></div>
<div>However, the comment implies that the second one should instead be this:<br></div>
<div> <br></div>
<div><span style="white-space:pre;"></span>public func unfold<T>(_ initialElement: T, applying: T -> T?) -> UnfoldSequence<T><br></div>
<div> <br></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> <br></div>
<div><span style="white-space:pre;"></span>public func unfold<T, State>(fromState initialState: State, applying: State -> (T, State)?) -> UnfoldSequence<T><br></div>
<div><span style="white-space:pre;"></span>public func unfold<T>(fromFirst initialElement: T, apply: T -> T) -> UnfoldSequence<T><br></div>
<div> <br></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> <br></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> <br></div>
<div><span class="font" style="font-family:Menlo">`sequence(_:, next:) -> AdHocSequence`</span><br></div>
<div> <br></div>
<div>might be a Swift acceptable solution. 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> <br></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> <br></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> <br></div>
<div>As for return type name, we could go ahead and use UnfoldSequence<T> 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).<br></div>
<div> <br></div>
<div>So given that, I'll suggest the following:<br></div>
<div> <br></div>
<div> func sequence<T>(initial: T, next: T -> T?) -> UnfoldSequence<T><br></div>
<div> func sequence<T, State>(state: State, next: (inout State) -> T?) -> UnfoldSequence<T><br></div>
<div> <br></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 -> (T, State)? in functionality but is less likely to produce unwanted COW copies.<br></div>
<div> <br></div>
<div>-Kevin Ballard<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>