Using Collection.split on end caps

I'm trying out my own version of Collection.split in an Xcode macOS playground, and I got a discrepancy:

let emptyLine = ""
let spaceChar: Character = " "
let spaceStr = " "
let test01 = spaceStr.lazy.split_10(separator: spaceChar)  // My custom lazy type
let test02 = emptyLine.lazy.split_10(separator: spaceChar)  // Same
Array(test01)  // ["", ""]
Array(test02)  // [""]
spaceStr.split(separator: spaceChar)  // []
emptyLine.split(separator: spaceChar)  // []

I thought that when you split, and you have a separator Element value at an end-cap, that you get an empty sub-sequence there. But the system-provided non-lazy version gives empty collections for both cases. How am I misinterpreting the Collection.split docs? (Or am I right and the StdLib authors who have it wrong? Is there a test suite for this (yet)?)

That is controlled by the omittingEmptySubsequences parameter:

If false , an empty subsequence is returned in the result for each pair of consecutive elements satisfying the isSeparator predicate and for each element at the start or end of the collection satisfying the isSeparator predicate. The default value is true.

let emptyLine = ""
let spaceChar: Character = " "
let spaceStr = " "
print(spaceStr.split(separator: spaceChar, omittingEmptySubsequences: false))
// ["", ""]
print(emptyLine.split(separator: spaceChar, omittingEmptySubsequences: false))
// [""]

A night of sleep makes a difference.

The "_10" part of split_10 indicates which extension of Ben Cohen's LazySplitCollection from his blog article I'm working on. The "1" means I added support for split limits while the "0" means support for skipping empty sequences was not added. From that, and the name of the supporting type:

LazyEmptyAdmittingSplitCollection<String>

you can see that I messed up the standard code I did my comparison with. I forgot that the StdLib split skips empty sub-sequences by default. Correcting that in the playground:

spaceStr.split(separator: spaceChar, omittingEmptySubsequences: false)  // ["", ""]
emptyLine.split(separator: spaceChar, omittingEmptySubsequences: false)  // [""]

means that I (seemingly) did get it right.

Thanks for seeing it too,@Martin

Terms of Service

Privacy Policy

Cookie Policy