I have a nested array in Swift, where each internal array could be of varying size:
let numbers = [[0], [1], [2, 3], [4]]
The problem is that I need to modify one entry based on a single index value (based essentially on the index of the flattened array; so in this example, each value is essentially its index).
I've got half the problem down with a flatMap call, but I'm not sure how to re-nest it afterwards, or whether I've taken the wrong approach in the first place.
func setValue(_ value: Int, index: Int, list: [[Int]]) -> [[Int]]
{
var output = numbers.flatMap { $0 }
output[index] = value
// TODO: Re-nest
return [output]
}
let output = setValue(42, index: 3, list: numbers)
print(output) // [[0, 1, 2, 42, 4]]
How do I make this output [[0], [1], [2, 42], [4]], and is there a more elegant (functional) way to achieve this?
[[Int]]supports