<div dir="ltr">I like this idea, as long as it is kept small in scope, only the introduction of a yield implemented as something conforming to SequenceType.<div><div><br></div><div>You can get fairly concise syntax already with anySequence and AnyGenerator:</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">func</span> fibonacci() -> <span style="color:rgb(112,61,170)">AnySequence</span><<span style="color:rgb(112,61,170)">Int</span>> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">return</span><span style="color:rgb(0,0,0)"> </span>AnySequence<span style="color:rgb(0,0,0)"><</span>Int<span style="color:rgb(0,0,0)">> { () -> </span>AnyGenerator<span style="color:rgb(0,0,0)"><</span>Int<span style="color:rgb(0,0,0)">> </span><span style="color:rgb(187,44,162)">in</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">var</span> (i, j) = (<span style="color:rgb(39,42,216)">0</span>, <span style="color:rgb(39,42,216)">1</span>)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">return</span><span style="color:rgb(0,0,0)"> </span>anyGenerator<span style="color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> (i, j) = (j, i + j)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">return</span> i</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></p><p style="margin:0px;line-height:normal">But state management is more involved.</p></div><div><br></div><div>To implement this I guess you'd have to look at each yield statement and work out what state it can have, something like this:<br></div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">func</span> test() -> <span style="color:rgb(112,61,170)">AnySequence</span><<span style="color:rgb(112,61,170)">Int</span>> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">var</span> j = <span style="color:rgb(39,42,216)">0</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">for</span> i <span style="color:rgb(187,44,162)">in</span> <span style="color:rgb(39,42,216)">1</span> ... <span style="color:rgb(39,42,216)">5</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> j += i</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">yield</span> j</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">yield</span> j</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">return </span>j+1</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></p><p style="margin:0px;line-height:normal">becomes:</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">func</span> test() -> <span style="color:rgb(112,61,170)">AnySequence</span><<span style="color:rgb(112,61,170)">Int</span>> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">return</span><span style="color:rgb(0,0,0)"> </span>AnySequence<span style="color:rgb(0,0,0)"><</span>Int<span style="color:rgb(0,0,0)">> { () -> </span>AnyGenerator<span style="color:rgb(0,0,0)"><</span>Int<span style="color:rgb(0,0,0)">> </span><span style="color:rgb(187,44,162)">in</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">var</span> state: <span style="color:rgb(79,129,135)">YieldState_test</span> = .<span style="color:rgb(49,89,93)">Yield0</span>(j: <span style="color:rgb(39,42,216)">0</span>, i: <span style="color:rgb(39,42,216)">1</span>)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(61,29,129)">anyGenerator</span> { () -> <span style="color:rgb(112,61,170)">Int</span>? <span style="color:rgb(187,44,162)">in</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">switch</span> state {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">case</span> <span style="color:rgb(187,44,162)">let</span> .Yield0(j: j, i: i):</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">let</span> j_ = i + j, i_ = i+<span style="color:rgb(39,42,216)">1</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> state = (i_ <= <span style="color:rgb(39,42,216)">5</span>) ? .<span style="color:rgb(49,89,93)">Yield0</span>(j: j_, i: i_) : .<span style="color:rgb(49,89,93)">Yield1</span>(j: j_)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">return</span> j_</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">case</span> <span style="color:rgb(187,44,162)">let</span> .Yield1(j: j):</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> state = .<span style="color:rgb(49,89,93)">Return0</span>(j)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">return</span> j</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">case</span> <span style="color:rgb(187,44,162)">let</span> .Return0(r):</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> state = .<span style="color:rgb(49,89,93)">Complete</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">return</span> r+<span style="color:rgb(39,42,216)">1</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">case</span> .Complete:</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(187,44,162)">nil</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">
</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p></div><div><br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Dec 12, 2015 at 10:20 PM, T.J. Usiyan via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">This feature (set) as sketched here by Joe is worthwhile, in my opinion. The use of catch fits in and effect neatly captures this class of behaviors.<div>+1 for `effect`</div><div>---</div><div>TJ</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Sat, Dec 12, 2015 at 4:17 AM, Joe Groff via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div style="word-wrap:break-word"><span><br><div><blockquote type="cite"><div>On Dec 11, 2015, at 6:26 PM, Jordan Rose via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Eh, I was trying to avoid grabbing another keyword, but I guess it's context-sensitive anyway.</div></div></blockquote></div><br></span><div>I've thought about this some. It might not have to be a keyword, if this were a generalized language feature. Anything that interrupts control flow and optionally resumes it later, such as 'throws', 'yields', and potentially also 'async', could be implemented as instances of algebraic effects. As a rough sketch of an idea, you could declare an effect and its operations:</div><div><br></div><div>effect throws { @noreturn operation throw (ErrorType) -> () }</div><div>effect yields<T> { operation yield (T) -> () }</div><div>effect awaits { operation await<T> (Promise<T>) -> T }</div><div><br></div><div>and 'catch' could be generalized to let you handle any effect operations that might be performed in the body of a block:</div><div><br></div><div>class Generator<T> {</div><div><span style="white-space:pre-wrap">        </span>var generator: () yields<T> -> ()</div><div><span style="white-space:pre-wrap">        </span>func next() -> T? {</div><div><span style="white-space:pre-wrap">                </span>do {</div><div> <span style="white-space:pre-wrap">                        </span>generator()</div><div><span style="white-space:pre-wrap">                        </span>return nil</div><div><span style="white-space:pre-wrap">                </span>} catch yield (let x) {</div><div><span style="white-space:pre-wrap">                        </span>generator = currentContinuation</div><div><span style="white-space:pre-wrap">                        </span>return x</div><div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">        </span>}</div><div>}</div><div><br></div><div>See Eff (<a href="http://www.eff-lang.org" target="_blank">http://www.eff-lang.org</a>) for an example of a language with this already implemented.</div><div><br></div><div>-Joe</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=3FGfocPbgxkNkmje7djckg9Iw-2BGYY3X6RxJ1tkUXKCpdyfs-2FQ6wsPiWsasUiN34N7QpC1VqX1dhUY5BX2NCx0wGd-2B97k6MUb-2BXjdz0OsJEFjXiU-2FJXvItzEXdEWfoBELF-2BWbN8qFKHXp88-2Fi6-2F3jsyvGSQfZxOo1o4YUoIVESWrjYC9otKRm9yjFHM1LjohwwLrqm-2BMvHKqdQcjukCSmGbhP-2Frme5oHfbVHRsBGE51o-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
<br></div></div><span class="">_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></span></blockquote></div><br></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=pQw7h83fWt3LLbgkfL4TSUL0weaZnVFZxDe5GShw4uR6iFm29ybOtOrMDepiqnDFH1qfjFgkETIMi6tvGtry2kFZ-2FsN0MMTFz0wRC62oN-2BRUflyD70SSGgSYy-2Bx2ORkeZMapCsxJn-2FqomJYZPklI3ULDIykhFClAHfdcJA-2FIbJih3P85r1Vf7ZXkCHHZc7Ecz2DCi0qzB4mai6LOcaoW4g-3D-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>