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! }
    }
}

Last updated