.allElementsSameLength
ๆชขๆฅๆฏๅฆๆๆๅ ็ด (ไธๅ่ณๆ) ้ฝ็ญ้ทใ
โญ๏ธ ๆณจๆ๏ผ้ๆฏ Collection ็ๆดๅฑ๏ผไธๆฏ Array ็ใ
๐พ ็จๅผ๏ผ replit
// extension primarily for array of arrays
extension Collection where Element: Collection {
/// all elements (rows) are of the some length
/// - `[[1,2], [3,4]].allElementsSameLength == true`
public var allElementsSameLength: Bool {
guard !isEmpty else { return true }
return dropFirst().allSatisfy { $0.count == first!.count }
}
}
๐พ ็จๅผ๏ผ replit
public func testArray_allElementsSameLength() {
// array of arrays
let data = [
["labe", "value", "type"],
["name", "joe", "String"],
["age", "8", "Int"]
]
let data2 = [
["labe", "value", "type"],
["name", "joe"],
["age", "8", "Int"]
]
let data3 = [[1,1], [3,4]]
let data4 = [[1,2,3]] // one row only
let data5 = [[Int]]() // no row
// print results
let items: [HasMirrors] = [
data.allElementsSameLength, // T
data2.allElementsSameLength, // F
data3.allElementsSameLength, // T
data4.allElementsSameLength, // T
data5.allElementsSameLength, // T
]
items.log()
}
Result:
// โ
// โ
// โ
// โ
// โ
is an extension for Collections.
similar to arr.allElementsEqual.
Last updated