Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swift-only homogeneousnessedness
extension SequenceType where Generator.Element: Equatable {
/// Checks every element in a sequence is equal to a given element
func all(element: Generator.Element) -> Bool {
return !contains { $0 != element }
}
/// Checks no element in a sequence is equal to a given element
func none(element: Generator.Element) -> Bool {
return !contains(element)
}
}
extension String {
var isHomogeneous: Bool {
return characters.first.map {
dropFirst(characters).all($0)
} ?? true
}
}
"aaa".isHomogeneous
"abc".isHomogeneous
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.