Skip to main content
fixed code to compile
Source Link

With help from the codereview community, I've written a function to flatten a generic nested array. I have it written several ways but want to focus on the following implementation:

func flatten (input: [Any]) -> [Any] {


    var outputArray = [Any] ()

    for i in 0..<input.count {
        let data = input[i];
        (data is Array[Any]) ? outputArray += flatten(input: data as! [Any]) : outputArray.append(data)

    }

    return outputArray

}

Is there a more "swifty" way to write this without using higher order functions?

Additionally, I've been reading about tail call optimization and wonder how I can make this tail call optimized?

With help from the codereview community, I've written a function to flatten a generic nested array. I have it written several ways but want to focus on the following implementation:

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 as! [Any]) : outputArray.append(data)

    }

    return outputArray

}

Is there a more "swifty" way to write this without using higher order functions?

Additionally, I've been reading about tail call optimization and wonder how I can make this tail call optimized?

With help from the codereview community, I've written a function to flatten a generic nested array. I have it written several ways but want to focus on the following implementation:

func flatten (input: [Any]) -> [Any] {


    var outputArray = [Any] ()

    for i in 0..<input.count {
        let data = input[i];
        (data is [Any]) ? outputArray += flatten(input: data as! [Any]) : outputArray.append(data)

    }

    return outputArray

}

Is there a more "swifty" way to write this without using higher order functions?

Additionally, I've been reading about tail call optimization and wonder how I can make this tail call optimized?

Post Reopened by Raystafarian, 200_success
edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

How to tail call optimize a generic flatten Generic array flattening function

added working code and restructured question to be less vague
Source Link

Hand-rolled function How to flattentail call optimize a generic nested arrayflatten function

I'm trying to writeWith help from the codereview community, I've written a simple hand-rolled function to flatten a generic 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 appendhave it written several ways but want to focus on the output array".following implementation:

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 as! [Any]) : 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 functionIs there a more "swifty" way to constantly unpack the array and it just gets stuck in the loop. Any suggestions on how I can avoidwrite this without using higher order functions?

The following code works just fineAdditionally, I've been reading about tail call optimization and I honestly can't figure outwonder how to get around my issue -

func flatten (input: [Any]) -> [Any] {
    
    var outputArray = [Any] ()

    for e in input {
        switch e {
        case let e as [Any]:
            outputArray += flatten(input: e)
        default:
            outputArray.append(e)
        }
    }
    return outputArray
}

Edit - figured out working code. Edited above.I can make this tail call optimized?

Hand-rolled function to flatten a generic nested array

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 as! [Any]) : 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?

The following code works just fine and I honestly can't figure out how to get around my issue -

func flatten (input: [Any]) -> [Any] {
    
    var outputArray = [Any] ()

    for e in input {
        switch e {
        case let e as [Any]:
            outputArray += flatten(input: e)
        default:
            outputArray.append(e)
        }
    }
    return outputArray
}

Edit - figured out working code. Edited above.

How to tail call optimize a generic flatten function

With help from the codereview community, I've written a function to flatten a generic nested array. I have it written several ways but want to focus on the following implementation:

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 as! [Any]) : outputArray.append(data)

    }

    return outputArray

}

Is there a more "swifty" way to write this without using higher order functions?

Additionally, I've been reading about tail call optimization and wonder how I can make this tail call optimized?

Got code to compile and changed it accordingly.
Source Link
Loading
Post Closed as "Not suitable for this site" by Phrancis, forsvarir, t3chb0t, Tunaki, Vogel612
Source Link
Loading