/// A two-dimensional matrix of memoized values.
///
/// These values are populated by a function from the coordinates of a given cell to the matrix’s element type.
struct Matrix {
init(width: Int, height: Int, compute: (Int, Int) -> A) {
var values: [Memo] = []
values.reserveCapacity(width * height)
for i in 0.. { compute(i, j) }
}
}
self.init(width: width, height: height, values: values)
}
let width: Int
let height: Int
let values: [Memo]
subscript (i: Int, j: Int) -> Memo? {
guard i < width && j < height else { return nil }
return values[i + j * height]
}
// MARK: Functor
func map(transform: A -> Other) -> Matrix {
return Matrix(width: width, height: height, values: values.map { $0.map(transform) })
}
// MARK: Implementation details
private init(width: Int, height: Int, values: [Memo]) {
self.width = width
self.height = height
self.values = values
}
}