The recent addition of chunks(ofCounts:)
to the Algorithms package is great. I wonder if we would consider a slight broadening to cover a repeating sequence of chunk sizes. My motivating example is that I wanted to write code somewhat like the following:
let str = "66ad8d6e211b33e890e8f8259126ee3a"
let uuidString = str
.uppercased()
.segments(ofLengths: [8, 4, 4, 4, 12]
.joined(separator: "-")
assert(uuidString == "66AD8D6E-211B-33E8-90E8-F8259126EE3A")
I realized that this segments(ofLengths:)
function was quite similar to chunks(ofCount:)
in the Algorithms package, except that it accepted a series of lengths instead of a single chunk size.
I ended up implementing it as an extension on Collection, and realized that it might be viable for inclusion in the Algorithms package, in a way that the implementation could also be used by chunks(ofCount:)
. My implementation would need a little bit of refining before I could contribute it, but mostly just things like adding @usableFromInline
and such. Is there interest in including this kind of functionality in the package? If so, any thoughts on naming? I currently have called it chunks(ofCounts:)
, but I've also considered chunks(ofLengths:)
as well.
A few more examples to clarify behavior:
// The chunking pattern repeats to the end of the input:
"abcdefg".chunks(ofCounts: [3, 1]) == ["abc", "d", "efg"]
// Passing a single count is equivalent to calling `chunks(ofCount:)`
"abcdefg".chunks(ofCounts: [3]) == ["abc", "def", "g"]
Thoughts?