Created
May 4, 2023 01:50
-
-
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
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
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