Skip to content

Instantly share code, notes, and snippets.

@HarshilShah
Created May 4, 2023 01:50
Show Gist options
  • Save HarshilShah/f3cc736b6e5c1512ea1f391a6d2b924e to your computer and use it in GitHub Desktop.
Save HarshilShah/f3cc736b6e5c1512ea1f391a6d2b924e to your computer and use it in GitHub Desktop.
Collection extensions to get all possible prefixes in decreasing or increasing order of length
public extension Collection {
func contractingPrefixes() -> some Collection<SubSequence> {
self.indices.reversed().lazy.map { index in
self[startIndex ... index]
}
}
func expandingPrefixes() -> some Collection<SubSequence> {
self.indices.lazy.map { index in
self[startIndex ... index]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment