I would like to avoid using swift-algorithms so here's my attempt at handling the brackets with just using Swift. It works for 2D and 3D arrays but brackets for higher dimensions are not captured.
func description<T>(array: [T], shape: [Int]) -> String { let chunkSize = shape.last ?? 1 let chunks = stride(from: 0, to: numbers.count, by: chunkSize).map { Array(numbers[$0..<min($0 + chunkSize, numbers.count)]) } let width = array.map { "\($0)".count }.max() ?? 0 let lines = chunks.map { row in row.map { element in (String(repeating: " ", count: width) + element.description).suffix(width) }.joined(separator: " ") } var descr = "" let dim = shape.count let lastRow = shape.reduce(1, *) / chunkSize let lastSubRow = shape.reversed()[1] for (n, line) in lines.enumerated() { let n = n + 1 if n == 1 { // First row brackets let prepend = String(repeating: "β‘ ", count: dim - 1) let append = String(repeating: " β€", count: dim - 1) descr += prepend + line + append + "\n" } else if n == lastRow { // Last row brackets let prepend = String(repeating: "β£ ", count: dim - 1) let append = String(repeating: " β¦", count: dim - 1) descr += prepend + line + append + "\n" } else if n % lastSubRow == 0 { // Last row brackets on subarray let prepend = String(repeating: "β ", count: dim - 2) let append = String(repeating: " β₯", count: dim - 2) descr += prepend + "β£ " + line + " β¦" + append + "\n" } else if n % lastSubRow == 1 { // First row brackets on subarray let prepend = String(repeating: "β ", count: dim - 2) let append = String(repeating: " β₯", count: dim - 2) descr += prepend + "β‘ " + line + " β€" + append + "\n" } else { let prepend = String(repeating:"β ", count: dim - 1) let append = String(repeating: " β₯", count: dim - 1) descr += prepend + line + append + "\n" } } return descr }
Here's a 2D example:
let numbers: [Float] = [1, 2, 3, 42.8, 5, 6] let shape = [2, 3] let d = description(array: numbers, shape: shape) print(d)
β‘ 1.0 2.0 3.0 β€ β£ 42.8 5.0 6.0 β¦
Here's a 3D example:
let numbers: [Float] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] let shape = [3, 2, 3] let d = description(array: numbers, shape: shape) print(d)
β‘ β‘ 1.0 2.0 3.0 β€ β€ β β£ 4.0 5.0 6.0 β¦ β₯ β β‘ 7.0 8.0 9.0 β€ β₯ β β£ 10.0 11.0 12.0 β¦ β₯ β β‘ 13.0 14.0 15.0 β€ β₯ β£ β£ 16.0 17.0 18.0 β¦ β¦
And here is a 4D example. Notice the brackets are not captured for the extra dimension.
let numbers: [Float] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48] let shape = [2, 3, 2, 4] let d = description(array: numbers, shape: shape) print(d)
β‘ β‘ β‘ 1.0 2.0 3.0 4.0 β€ β€ β€ β β β£ 5.0 6.0 7.0 8.0 β¦ β₯ β₯ β β β‘ 9.0 10.0 11.0 12.0 β€ β₯ β₯ β β β£ 13.0 14.0 15.0 16.0 β¦ β₯ β₯ β β β‘ 17.0 18.0 19.0 20.0 β€ β₯ β₯ β β β£ 21.0 22.0 23.0 24.0 β¦ β₯ β₯ β β β‘ 25.0 26.0 27.0 28.0 β€ β₯ β₯ β β β£ 29.0 30.0 31.0 32.0 β¦ β₯ β₯ β β β‘ 33.0 34.0 35.0 36.0 β€ β₯ β₯ β β β£ 37.0 38.0 39.0 40.0 β¦ β₯ β₯ β β β‘ 41.0 42.0 43.0 44.0 β€ β₯ β₯ β£ β£ β£ 45.0 46.0 47.0 48.0 β¦ β¦ β¦
Any suggestions on how to print the brackets for higher dimensions without using the swift-algorithms package?