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