I'm trying to write a simple hand-rolled function to flatten a nested array. My code works fine as a switch statement, but not when I'm recursively calling it within a "for i in 0..arr.count" loop. I look at each element in the passed in array and say "if this is an array, call the function and pass this in, else append to the output array".
func flatten (input: [Any]) -> [Any] {
var outputArray = [Any] ()
for i in 0..<input.count {
let data = input[i]; (data is Array) ? outputArray += flatten(input: [data]) : outputArray.append(data)
}
return outputArray }Because my function argument must be of type [Any], I'm forced to pass the "data" variable back into it as [data]. This just forces the function to constantly unpack the array and it just gets stuck in the loop. Any suggestions on how I can avoid this?