LAST CHANCE: Save 50% on all my Swift books and bundles! >>

SOLVED: SOLVED: Solving Day 9 challenge with limited information

Forums > 100 Days of SwiftUI

NOTE: This query contains a spoiler for the Day 9 so do not proceed if you haven't finished the challenge.

I just finished the Day 9 challenge and worked through the solution video. I was a bit stumped because even though Paul works through filter, sort, and map in earlier videos, he applies them in the solution in a way that I wasn't prepared for. Here's the code for context:

let filteredArray = luckyNumbers.filter { $0.isMultiple(of: 2) == false }.sorted().map { "\($0) is a lucky number."}

So this leaves me asking a couple questions:

  1. Where did Paul mention in previous videos that we could chain methods to the ends of closures? As far as I can recall I haven't seen this demonstrated yet in the first 9 days and, frankly, I would have never thought to do this.
  2. I felt a little discouraged because I put in the effort to try and solve the challenge but my solution was pretty far from Paul's. How can I improve my thinking as a developer when my own solution looks radically different?

It's helpful to know that I can do these things now, but I'd like to know that I'm a bit more capable before tackling the challenge. Feedback is welcome.

3      

I just finished day 9 and I also didn't think of that. My code did assigned temporarry variables

let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]

let luckyNumbersEven = luckyNumbers.filter {!$0.isMultiple(of: 2)}
let luckyNumbersOrder = luckyNumbersEven.sorted()
var luckyNumberStrings = luckyNumbersOrder.map {"\($0)"}

for i in 0..<luckyNumberStrings.count{
    print("\(luckyNumberStrings[i]) is a lucky number.")
}

I don't think you should feel dissapointed, becuase this ckecpoitns are supposed to be based on knowledge we already have, and he hadn't taught that. I wouldn't have come with that soultion either. And tbh I had a really hard time coming with my soultion in the first place because he doesn't really explain map and sorted very well.

EDIT: He did mentioned how to chained them in the hints.

4      

@burr asks

Where did Paul mention in previous videos that we could chain methods to the ends of closures?

He provided this technique in the HINTS section of Checkpoint 5.

  1. To chain these functions, use luckyNumbers.first { }.second { }, obviously putting the real function calls in there.

By the way, you're not chaining methods to the end of closures. You're chaining a method to a type!

// oddNumbers is an array of Ints!
// Note that BOTH sides of the equal signs are "array of ints"
let oddNumbers = luckyNumbers.filter {!$0.isMultiple(of: 2)}

// Note! You chained the sorted() method to an array of Ints.
let sortedOddNumbers = oddNumbers.sorted()

// Look at this another way

// Look closely!
// Yes, there is a closure here. But the sorted() method isn't evaluated 
// until AFTER the filter is applied.
// AFTER the filter is applied, Swift now only sees an array of Integers.
// The sorted() method is applied to the filtered results which is an array of Integers.
let sortedOddNumbers = luckyNumbers.filter{!$0.isMultiple(of:2)}.sorted()

4      

I wish i could find the video. I think it was in one of @twoStraws' older Swift on Sundays episode.

In anycase, @twoStraws was challenged by a viewer as to why he didn't just jump straight to the "correct way" for coding something or other.

@twoStraws encouraged the viewer to read about "Wittgenstein's ladder"

See -> Wiki: Wittgenstein's ladder

In short, @twoStraws notes it's sometimes necessary to climb a ladder to gain a certain vantage point. Once the understanding is clear, you can destroy the ladder and never climb it again. You can see there are alternate paths to the same point. However, you may not appreciate the alternative paths without first having climbed that initial ladder. Also, it's time to build a new ladder and go higher.

@twoStraws does this in many of his videos. If you code along, you'll eventually find yourself erasing everything you just spent 27 minutes building. This is done for a very good reason. Follow along with the program!

In Challenge 5, it's perfectly cromulent to solve the exercise as @Toro demonstrated. But once you've seen that path to the answer, you'll appreciate how elegant chaining methods is!

You just had the ladder knocked out from under you. So now you'll be more alert to future code snips where this is very common.

Keep Coding!

4      

You might also see them on separate line but this is fact the same as adding to the end to end. I can be easier to read but no-one will tell you!

let filteredArray = luckyNumbers.filter { $0.isMultiple(of: 2) == false }
                                .sorted()
                                .map { "\($0) is a lucky number."}

You will see this alot in SwiftUI, Combine framework, etc

3      

Thank you all!

3      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.