3 ms·
I like to use recursion when I can't predict the number of iterations necessary to do the job. For example, a program that calculates the total size of a folder
by SwiftyBug 7y ago
I like to use recursion when I can't predict the number of iterations necessary to do the job. For example, a program that calculates the total size of a folder. With recursion I can easily iterate through all levels of the tree and get their sizes.
I've used the same approach recently when I wrote a GraphQL query generator in Swift. I recursively iterate through an array of dictionaries, each being able to also hold an array of dictionaries of it's own, each being able to also hold an array of dictionaries of it's own, and so on. In the end all I end up with is single a string.
Maybe it's not exactly more efficient, but it was certainly much easier for me.