collection.allElementsEqual
๐พ ็จๅผ๏ผ replit
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// โ Collection + .allElementsEqual โ
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
extension Collection where Element: Equatable {
/// ```
/// [1,1,1].allElementsEqual == true
/// ```
public var allElementsEqual: Bool {
// always true for empty arrays
guard first != nil else { return true }
return dropFirst().allSatisfy { $0 == first! }
}
}
[1, 2, 3].allElementsEqual, // โ
[1, 1, 1].allElementsEqual, // โ
[1].allElementsEqual, // โ
([] as! [Int]).allElementsEqual, // โ
["ya", "ya", "ya"].allElementsEqual, // โ
Last updated