<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">On more thing:<div class=""><br class=""></div><div class="">The result type of my `random()` extension is optional. This might not be ideal. It is not an expected and likely case that `random()` returns nil. I would personally prefer it to be non-optional and crash if the array is empty, just like it would if you tried to reference a non-existing index in the collection. So, the version I actually prefer is:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">RandomAccessCollection</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> random() -> Iterator.Element {</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">precondition</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">></span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, </span><span style="font-variant-ligatures: no-common-ligatures" class="">"There is no element to random pick."</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129);" class=""> </div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> offset = </span><span style="font-variant-ligatures: no-common-ligatures" class="">arc4random_uniform</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">numericCast</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">))</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> i = </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">index</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">startIndex</span><span style="font-variant-ligatures: no-common-ligatures" class="">, offsetBy: </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">numericCast</span><span style="font-variant-ligatures: no-common-ligatures" class="">(offset))</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">[i]</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On Oct 13, 2016, at 4:08 AM, Jean-Denis Muys <<a href="mailto:jdmuys@gmail.com" class="">jdmuys@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Thank you for this Hooman, I think this is what I was looking for.<div class=""><br class=""></div><div class="">JD</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Thu, Oct 13, 2016 at 4:26 AM, Hooman Mehr <span dir="ltr" class=""><<a href="mailto:hooman@mac.com" target="_blank" class="">hooman@mac.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><span class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Oct 12, 2016, at 3:21 AM, Jean-Denis Muys via swift-users <<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>> wrote:</div><br class="m_5433486535750080024Apple-interchange-newline"><div class=""><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class="m_5433486535750080024Apple-interchange-newline">But this is not very DRY.</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">What would be a more idiomatic way?</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div></div></blockquote><br class=""></div></span>The more idiomatic way is to look at API design in a new way. Note these points:<div class=""><br class=""></div><div class="">1. `Countable` variant is preferred when you want to deal with integer ranges as it more closely matches the element type.</div><div class="">2. Both countable range variants share a common protocol conformance already: `RandomAccessCollection`</div><div class="">3. Swift API design prefers member functions to free functions.</div><div class=""><br class=""></div><div class="">Hence a more idiomatic (Swifty) API would probably be something like this:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures" class="">RandomAccessCollection</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="m_5433486535750080024webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> random() -> Iterator.Element? {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="m_5433486535750080024webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">guard</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="">count</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="">></span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="">0</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">else</span><span style="font-variant-ligatures:no-common-ligatures" class=""> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">nil</span><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="m_5433486535750080024webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(62,30,129)" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> offset = </span><span style="font-variant-ligatures:no-common-ligatures" class="">arc4random_uniform</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures:no-common-ligatures" class="">numericCast</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><wbr class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="">count</span><span style="font-variant-ligatures: no-common-ligatures;" class="">))</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="m_5433486535750080024webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> i = </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="">index</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="">startIndex</span><span style="font-variant-ligatures:no-common-ligatures" class="">, offsetBy: </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81" class="">numericCast</span><span style="font-variant-ligatures:no-common-ligatures" class="">(offset))</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="m_5433486535750080024webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">self</span><span style="font-variant-ligatures:no-common-ligatures" class="">[i]</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div class="">Using the above, both cases work and there is no repetition:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="">4</span><span style="font-variant-ligatures:no-common-ligatures" class="">..<</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="">10</span><span style="font-variant-ligatures:no-common-ligatures" class="">).</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="">random</span><span style="font-variant-ligatures:no-common-ligatures" class="">()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="">4</span><span style="font-variant-ligatures:no-common-ligatures" class="">...</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8" class="">9</span><span style="font-variant-ligatures:no-common-ligatures" class="">).</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="">random</span><span style="font-variant-ligatures:no-common-ligatures" class="">()</span></div></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div class="">It also makes a lot more possible: </div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> people = [</span><span style="font-variant-ligatures:no-common-ligatures" class="">"David"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, </span><span style="font-variant-ligatures:no-common-ligatures" class="">"Chris"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, </span><span style="font-variant-ligatures:no-common-ligatures" class="">"Joe"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, </span><span style="font-variant-ligatures:no-common-ligatures" class="">"Jordan"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, </span><span style="font-variant-ligatures:no-common-ligatures" class="">"Tony"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">]</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> winner = </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">people</span><span style="font-variant-ligatures:no-common-ligatures" class="">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d" class="">random</span><span style="font-variant-ligatures:no-common-ligatures" class="">()</span></div></div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></body></html>