1 /** 2 Creates a range consisting of chunks of another range 3 4 Differences_between: 5 <li>$(LINK2 https://dlang.org/library/std/range/chunks.html, phobos.std.range.chunk) - treats `0` as a valid chunk size 6 <li>$(LINK2 https://lodash.com/docs/4.17.5#chunk, lodash.chunks) - none intended 7 */ 8 module ddash.range.chunk; 9 10 /// 11 unittest { 12 assert([1, 2, 3].chunk(0).equal((int[][]).init)); 13 assert([1, 2, 3].chunk(1).equal([[1], [2], [3]])); 14 } 15 16 import ddash.common; 17 18 /** 19 Creates a range of ranges of length `size`. If the range can't be split evenly, 20 the final `chunk`` will be the remaining elements. 21 22 Params: 23 range = An input range 24 size = chunk size 25 26 Returns: 27 Range of chunks 28 29 Since: 30 0.0.1 31 */ 32 auto chunk(Range)(Range range, size_t size) if (from!"std.range".isInputRange!Range) { 33 import std.range: chunks, takeNone; 34 if (size) { 35 return range.chunks(size); 36 } else { 37 return range.chunks(1).takeNone; 38 } 39 }