|
Hi All, I have the following code which uses a switch to return a dictionary key/value array based on a switch condition set elsewhere, at the moment as you can see, I have to use multiple for loops to illiterate over the indices of the values I'm looking for and it seems to me to be really inefficient as the for loops are used multiple times. I'm sure there is a better and more efficient approach to this, but I can't see it and I do have the need to use the function multiple times one after the other. Could anyone help with a better more efficient solution please? My code:
|
|
I haven't used all your structures. You could consider this, and use the princples behind it which are
|
|
Many thanks @Greenamberred, I have considered and dabbled using reduce and filter in my project and I did get it to (mostly) work however the issue that concerns me the most is having to illiterate over all items every time the function is called be this using a filter or in indices and I'm wondering if there is a way of "caching" the initial fetch so that on the next call it can just update that "cached" version. Many Thanks, Nigel |
|
I wouldn't worry about it unless you notice it impacting performance. In which case you can use Xcode's tools to see where the slowdowns are and fix things accordingly. Unless you are dealing with absolutely enormous numbers of items, you really shouldn't see any problems; the standard library collection and sequence methods are pretty well optimized. |
|
And just by way of illustration, here's a solution I worked up. Obviously, I don't have your full code, so I had to guess and fill in some blanks when it came to your data structures. Shouldn't really matter, I don't think, as the principles are the same. You first have to create a method on the
And let's do some testing...
|
|
Thank you @roosterboy! You've recreated my structure really well to be fair and that was the solution I was looking for... I had tried using filter and reduce and it worked (mostly) but the obvious bit I missed was to check for the weekday case first and go from there! I also appreciate that the execution time is always going to vary with any loop you do over any number of items as it's all dictated by the length of the array and the big O notation of that array. Thank you both @roosterboy and @Greenamberred you have both been of great help and to be honest you have pointed out the obvious to me which is what I was missing! Thank you both again! |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.