<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="">Hi Daryle,<div class=""><br class=""></div><div class="">I think this is a perfect case for an addition to the existing Lazy suite (and IMO would make a good evolution proposal too, as this kind of “chunked” splitting is a useful feature). You’re right though, there isn’t a good guide out there for how to do this (calling any intrepid blogger who wants to write one…)</div><div class=""><br class=""></div><div class="">Here’s a sketch of how it would fit into lazy. This is with the latest toolchain from master, which makes things a LOT easier now that IndexDistance is dead. It’s slightly inefficient because it re-uses Index from Base for its index, it would be more efficient to use a start/end pair for the chunks but that’s a lot more code :)</div><div class=""><br class=""></div><div class="">What’s also interesting is you could (with conditional conformance now that’s landed) make it a RandomAccessCollection if the base were random access, but <i class="">not</i>&nbsp;if the base is bidirectional, because you need to be able to calculate the size of the last element in constant time.</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">struct</span> LazyChunkedCollection&lt;Base: <span style="color: #3495af" class="">Collection</span>&gt; {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #0433ff" class="">let</span> base: <span style="color: #3495af" class="">Base</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #0433ff" class="">let</span> size: <span style="color: #3495af" class="">Int</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; 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: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(4, 51, 255);" class="">extension</span><span style="color: rgb(0, 0, 0);" class=""> </span><font color="#3495af" class="">LazyChunkedCollection</font><span style="color: rgb(0, 0, 0);" class="">:</span><font color="#3495af" class="">&nbsp;Collection</font><span style="color: rgb(0, 0, 0);" class="">&nbsp;{</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #0433ff" class="">typealias</span> Index = <span style="color: #3495af" class="">Base</span>.<span style="color: #3495af" class="">Index</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #0433ff" class="">typealias</span> Element = <span style="color: #3495af" class="">Base</span>.<span style="color: #3495af" class="">SubSequence</span></div><p style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #0433ff" class="">var</span> startIndex: <span style="color: #3495af" class="">Index</span> { <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">base</span>.<span style="color: #3495af" class="">startIndex</span> }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #0433ff" class="">var</span> endIndex: <span style="color: #3495af" class="">Index</span> { <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">base</span>.<span style="color: #3495af" class="">endIndex</span> }</div><p style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #0433ff" class="">subscript</span>(i: <span style="color: #3495af" class="">Index</span>) -&gt; <span style="color: #3495af" class="">Element</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">base</span>[i..&lt;<span style="color: #3495af" class="">index</span>(after: i)]</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><p style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #0433ff" class="">func</span> index(after i: <span style="color: #3495af" class="">Index</span>) -&gt; <span style="color: #3495af" class="">Index</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">base</span>.<span style="color: #3495af" class="">index</span>(i, offsetBy: <span style="color: #3495af" class="">size</span>, limitedBy: <span style="color: #3495af" class="">base</span>.<span style="color: #3495af" class="">endIndex</span>) ?? <span style="color: #3495af" class="">base</span>.<span style="color: #3495af" class="">endIndex</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font color="#008f00" face="Menlo" class=""><span style="font-size: 11px;" class="">// Keep the laziness transitive</span></font></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(4, 51, 255);" class="">extension</span>&nbsp;<font color="#3495af" class="">LazyChunkedCollection</font>:<font color="#3495af" class="">&nbsp;</font><span style="color: rgb(52, 149, 175);" class="">LazyCollectionProtocol</span>&nbsp;{ }</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; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);" class="">// Extending this protocol will make this feature appear on the .lazy property of Collection</div></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 149, 175); background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">extension</span><span style="color: #000000" class=""> </span>LazyCollectionProtocol<span style="color: #000000" class=""> {</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class="">&nbsp; &nbsp;&nbsp;</span>// Elements/elements are a way to avoid redundent wrapping</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #0433ff" class="">func</span> chunked(into chunksOf: <span style="color: #3495af" class="">Int</span>) -&gt; <span style="color: #3495af" class="">LazyChunkedCollection</span>&lt;<span style="color: #3495af" class="">Elements</span>&gt; {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0433ff" class="">return</span> <span style="color: #3495af" class="">LazyChunkedCollection</span>(base: <span style="color: #3495af" class="">elements</span>, size: chunksOf)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #0433ff" class="">let</span> l = <span style="color: #3495af" class="">Array</span>(0..&lt;10).<span style="color: #3495af" class="">lazy</span>.<span style="color: #3495af" class="">chunked</span>(into: 3)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; 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="">l</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #0433ff" class="">for</span> y <span style="color: #0433ff" class="">in</span> x {</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3495af" class="">print</span>(y, terminator: <span style="color: #b4261a" class="">","</span>)</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #3495af" class="">print</span>()</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; 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=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">0,1,2,</b></span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">3,4,5,</b></span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">6,7,8,</b></span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">9,</b></span></div></div></div><div><br class=""><blockquote type="cite" class=""><div class="">On Dec 29, 2017, at 18:40, Daryle Walker via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">I’ve been playing around with iterators/sequences/collections that partition to a set stride. For instance, given a collection with 16 elements and a stride argument of 3; I can generate a collection with 6 elements, the first 5 are sub-collections of count 3 and the last a sub-collection of count 1.</div><div class=""><br class=""></div><div class="">Now I’m wondering if I put these in a library on GitHub, should they be structured under the standard library’s “lazy” group of procedures? Or just separate types?</div><div class=""><br class=""></div><div class="">Oh, is there a modern guide for making lazy algorithm types? All the ones I’ve seen so far are from the Swift 1.0 days (when the algorithms were free functions instead of extensions of Sequence/Collection). Yes, I know I’m using busted vs. modern for a language only a few years old.</div><br class=""><div class="">
<div style="letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">—&nbsp;</div><div class="">Daryle Walker<br class="">Mac, Internet, and Video Game Junkie<br class="">darylew AT mac DOT com&nbsp;</div></div>
</div>
<br class=""></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>