<div dir="ltr">I’m not sure if you wanted to stick with the pure functional approach, but here’s an alternative that uses Range<Int> to take care of most of the work.<br><div><br></div><div><div>func selectionSort(_ array: [Int]) -> [Int] {</div><div> guard let minValue = array.min(), let index = array.index(of: minValue) else {</div><div> return []</div><div> }</div><div><br></div><div> let ranges = [0..<index, index.advanced(by: 1)..<array.endIndex]</div><div> return [minValue] + selectionSort(ranges.flatMap { array[$0] })</div><div>}</div></div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 28, 2016 at 7:20 PM, Aaron Bohannon via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@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">I think that's about as simple as you can make it unless you allow yourself to remove more than one element at a time when the minimum appears more than once.<div><br></div><div>Here's the question I find interesting: what's the simplest way to change that code into a version based on lazy collections? After all, there would arguably be some real practical value to a lazy recursive selection sort in cases where only a relatively small prefix of the resulting collection was expected to be needed. I took a stab at making your code lazy but quickly realized that it wasn't going to be as easy as I thought.</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 28, 2016 at 8:50 AM, Adriano Ferreira via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@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 style="word-wrap:break-word">Hi everyone!<div><br></div><div>I’m experimenting with this functional selection sort code and I wonder if anyone could help me simplify the portion indicated below.</div><div><br></div><div><br></div><div><span style="color:rgb(0,132,0);font-family:Menlo;font-size:11px">// Swift 3</span></div><div><span style="color:rgb(0,132,0);font-family:Menlo;font-size:11px"><br></span></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">func</span><span> selectionSort(</span><span style="color:#bb2ca2">_</span><span> array: [</span><span style="color:#703daa">Int</span><span>]) -> [</span><span style="color:#703daa">Int</span><span>] {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#bb2ca2">guard</span><span> array.</span><span style="color:#703daa">count</span><span> > </span><span style="color:#272ad8">1</span><span>, </span><span style="color:#bb2ca2">let</span><span> minElement = array.</span><span style="color:#3d1d81">min</span><span>() </span><span style="color:#bb2ca2">else</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#bb2ca2">return</span><span> array</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#bb2ca2">let</span><span> indexOfMinElement = array.</span><span style="color:#3d1d81">index</span><span>(of: minElement)!</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br><span></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:#000000"> </span><span>// All of this just to filter out the first smallest element and return the rest</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><span> </span><span style="color:rgb(0,132,0)">// Also tried ‘suffix(from:)' here, but couldn’t make it work properly</span></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#bb2ca2">let</span><span> rest = array.</span><span style="color:#3d1d81">enumerated</span><span>()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> .</span><span style="color:#3d1d81">filter</span><span>({ index, </span><span style="color:#bb2ca2">_</span><span> </span><span style="color:#bb2ca2">in</span><span> index != indexOfMinElement })</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> .</span><span style="color:#3d1d81">map</span><span>({ </span><span style="color:#bb2ca2">_</span><span>, element </span><span style="color:#bb2ca2">in</span><span> element })</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#bb2ca2">return</span><span> [minElement] + </span><span style="color:#31595d">selectionSort</span><span>(rest)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><br></span></div><div><br></div>By the way, it feels really weird to chain method calls like this in Swift 3, particularly due to the mixing of terms of art (e.g. “filter” and “map”) with other methods that follow the -ed/-ing rules from the API guidelines (e.g. enumerated).<div><br></div><div>Best,<br><br><div style="margin:0px;line-height:normal"><span>— A</span></div></div></div><br>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div>