Skip to content

Instantly share code, notes, and snippets.

@airspeedswift
Last active August 29, 2015 14:23
Show Gist options
  • Save airspeedswift/635fb85d73149f9b1413 to your computer and use it in GitHub Desktop.
Save airspeedswift/635fb85d73149f9b1413 to your computer and use it in GitHub Desktop.
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