|
Just finished reviewing Day 9 Closures and was hoping someone could explain how the captainFirstSorted works. I understand the coding syntax but am not sure how it all works. The array "team" has four strings but the captainFirstSorted function has only two strings as parameters. Does the code somehow iterate through the array? Is that taken care of by the sort() function? Thanks.
|
|
Sometimes you need a different pairs of glasses to see the problem more clearly. Let's look at a different example. Dump all the forks, spoons, and ONE knife on your kitchen table. How would you sort these? What's a good way to sort knives, forks, and spoons? There isn't a way, is there? So you have to provide the rules. If you decide to sort them alphabetically, you will be comparing the whole pile, but will do it two pieces at a time. Is this fork greater than this spoon? Is this knife greater than this fork? As you look at the pile and sort them you are making this comparison over and over and over and over again. You keep doing this comparison until they are all sorted from forks, the knife, then all the spoons. Now, add a new rule. The knife should ALWAYS be sorted on the top. So introduce a new comparison in your rule. IF the object on the left is a knife, then it is GREATER than the other object, end of story. If the object on the right is a knife, then it is GREATER than the other object, full stop. In short, the rules in the sorting algorithm state that if you give me two pieces of cutlery, I should sort them alphabetically. UNLESS one of them is a knife. If either is a knife then it is sorted higher than the other item.
|
|
If it helps, the function signature for
As you can see, the parameter name for the closure you pass to |
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Thank you both for your help. Much appreciated. I hope you don't mind but I have a couple of questions for futher clarification. 1) @Obelix , when you say "If the object on the right is a knife, then it is GREATER than the other object, full stop.". What do you mean by "full stop". 2) The function is defined to return a Bool. The If statements obviously return a Bool; however, the final return statement is "return cutleryOne < cutleryTwo //Otherwise, sort alphabetically". I don't recognize that as a Bool. Is it? |
|
So |
|
|
|
Come for the Swift, get a free English lesson on the side!Haus asks:
Full Stop is a Briticism. Maybe it's said elsewhere in the world, but used quite a bit in Britain. At the end of sentences you'll see a small dot of punctuation. In US and Canada, it's call a period. Not sure what's used in Australia, New Zealand and other english speaking locales. In Britain, however, it's call a full stop. When used in the context above, it's usually meant to add emphasis to the statement. Often the implication is there is nothing more to be said. So when I said
I was pointing out that no other evaluation was necessary. The knife object is always greater than anything else it is compared to. Nothing more to say. |
|
Howdy, @roosterboy
Paul doesn't cover rethrows, but are you saying the func returns a Bool & can also rethrow an Element & said element is: cutleryOne < cutleryTwo I came here for the exact same question as OP as my brain was not understanding how this code worked...still not 100% clear, but we're getting there. |
|
Hi @Baron_Blackbird - ignore the throws and rethrows for now. Let's for one second pretend that it's not there. You then get the definition of sorted as
so as you can see, The function Hope this helps clear things up |
|
Hey So You can see that the
meaning that it is a function that takes two items of type You will only ever see the This is useful because it allows you to skip using
Or So, looking again at the definition for
we can describe it as:
|
|
Here is my playing around, with Swift's version of closures. If it helps anyone. I did one closure, that you pass a closure into. Just to see the errors I could trip over.
gives output
|
|
Sorry to ressurect an old thread but this has been hurting my head too, but I think I now understand how the name1, name2 selection is working.. (maybe) (apologies for the text results posted as code, it was the simplest way to illustrate what I seeing) @Obelisk
Are you saying that this will carry out the sort as many times as it needs and will choose any 2 strings to compare if they don't match the specified name1 or name2 ? using the print method from @dsnovaes (thank you for suggesting this method) the result I got was :
For fun I added another name to the list "Helen" and instead of it checking through 4 times it now did this 8 times.
I then took Suzanne out of the captain seat and added in a ficticous "Beryl", who isn't in the array at all and it then went through the sort process 11 times
result
|
|
@Natalie continues an old thread with a great follow-up question:
Yes!
Not quite! If your array has 1,000 name elements, SwiftUI will send two names at a time into your function to evaluate. Which two? We don't really know. But let's assume it's somewhat smart about the order. For instance, if it sends You didn't write the sorted() function. This is built into Swift's Array() structure, but you need to provide the guts of this function! Take a peek at Apple's documentation for Array:
In this initializer, Swift says it will give you just TWO elements in your vast array. (Could be thousands of array elements!) You provide the rule to determine which element is GREATER than the other element. Are glasses > contact lenses? You can make up your own sorting rules and provide them as a FUNCTION that takes two array elements and returns a BOOL. Swift doesn't care how you sort your glasses or toothbrushes. We also don't know the order in which it selects the items from your array. Just be comfortable with the idea that your FUNCTION may be called hundreds of times before SwiftUI returns the final, sorted a array! Nice to revisit a tough topic! Keep Coding! |
|
@Obelix, Thank you,(and sorry for the spell check mis-correcting me earlier and resulting in a mis-spell of your name) learning swift becomes a little clearer each day........at least until Paul adds something new for me to wrap my head around, lol |
|
Thanks @Obelix! Using the original array from the lesson and the adding a few print statements to the closure:
I was super confused as to why Suzane and Gloria were selected as name1 and name2, respectively, on my first loop through the closure. |
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.