<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Oct 4, 2017, at 19:26, Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="auto" 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; -webkit-text-stroke-width: 0px;" class="">extension Data {</div><div dir="auto" 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; -webkit-text-stroke-width: 0px;" class=""> static func random(byteCount: Int) -> Data</div><div dir="auto" 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; -webkit-text-stroke-width: 0px;" class="">}</div><div dir="auto" 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; -webkit-text-stroke-width: 0px;" class=""><br class=""></div></div></blockquote></div><br class=""><div class=""><br class=""></div><div class="">Instead of methods on specific concrete types, I’d prefer to have a Sequence or Collection-conforming type that you could compose with existing operations like init from a Sequence or replaceSubrange:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0); background-color: rgb(255, 255, 255);" class="">// Just a rough sketch...</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">struct</span><span style="color: #000000" class=""> RandomNumbers<T: </span>FixedWidthInteger<span style="color: #000000" class="">>: </span>RandomAccessCollection<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">let</span> _range: <span style="color: #3495af" class="">ClosedRange</span><<span style="color: #3495af" class="">T</span>></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">let</span> _count: <span style="color: #3495af" class="">Int</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> startIndex: <span style="color: #3495af" class="">Int</span> { <span style="color: #0433ff" class="">return</span> 0 }</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">var</span> endIndex: <span style="color: #3495af" class="">Int</span> { <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">_count</span> }</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">subscri</span><span style="text-decoration: underline ; color: #0433ff" class="">p</span><span style="color: #0433ff" class="">t</span>(i: <span style="color: #3495af" class="">Int</span>) -> <span style="color: #3495af" class="">T</span> { <span style="color: #0433ff" class="">return</span> _range.random() }</div><p style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 15px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">init</span>(of: <span style="color: #3495af" class="">T</span>.Type, count: <span style="color: #3495af" class="">Int</span>) {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #3495af" class="">_range</span> = <span style="color: #3495af" class="">T</span>.<span style="color: #3495af" class="">min</span>...<span style="color: #3495af" class="">T</span>.<span style="color: #3495af" class="">max</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #3495af" class="">_count</span> = count</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">init</span>(count: <span style="color: #3495af" class="">Int</span>) {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">self</span> = <span style="color: #3495af" class="">RandomNumbers</span>(of: <span style="color: #3495af" class="">T</span>.<span style="color: #0433ff" class="">self</span>, count: count)</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>// possibly a constructor that takes a specific range</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #0433ff" class="">init</span><R: <span style="color: #3495af" class="">RangeExpression</span>>(in: <span style="color: #3495af" class="">R</span>, count: <span style="color: #3495af" class="">Int</span>) <span style="color: #0433ff" class="">where</span> <span style="color: #3495af" class="">R</span>.<span style="color: #3495af" class="">Bound</span> == <span style="color: #3495af" class="">T</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #008f00" class="">// etc</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">for</span> x <span style="color: #0433ff" class="">in</span> <span style="color: #3495af" class="">RandomNumbers</span>(of: <span style="color: #3495af" class="">UInt8</span>.<span style="color: #0433ff" class="">self</span>, count: 10) {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #3495af" class="">print</span>(x)</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">let</span> d = <span style="color: #3495af" class="">Data</span>(<span style="color: #3495af" class="">RandomNumbers</span>(count: 10))</div></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">var</span> a = <span style="color: #3495af" class="">Array</span>(0..<10)</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class="">a<span style="color: #000000" class="">.</span>replaceSubrange<span style="color: #000000" class="">(5..., with: </span>RandomNumbers<span style="color: #000000" class="">(count: 5))</span></div></div><div class=""><span style="color: #000000" class=""><br class=""></span></div><div class="">The tricky thing is that making it a Collection violates the (admittedly unwritten) rule that a collection should be multi-pass, in the sense that multiple passes produce different numbers. Then again, so can lazy collections with impure closures. At least it isn’t variable length (unlike a lazy filter). And making it a collection of defined size means it can be used efficiently with things like append and replaceSubrange which reserve space in advance</div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>