Created
January 24, 2020 02:56
-
-
Save tangyumeng/70810826ac3efa8829ed514f813fb83a to your computer and use it in GitHub Desktop.
已排序数组,以相同元素为组拆分数组
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array { | |
func split(where condation: (Element, Element) -> Bool) -> [[Element]] { | |
var result: [[Element]] = self.isEmpty ? [] : [[self[0]]] | |
for (previous, current) in zip(self, self.dropFirst()) { | |
if (condation(previous, current)) { | |
result.append([current]) | |
} else { | |
result[result.endIndex - 1].append(current) | |
} | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment