TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Hacking with Swift Day 8 CHECK POINT 5

Forums > SwiftUI

Hey guys,

I am currently on day 8 and I am a bit overwhelmed with closures.

In checkpoint 5, our job is to:

  1. Filter out any numbers that are even
  2. sort the array in ascending order
  3. map them to strings in the format "7 is a lucky number"
  4. print the resulting array, one item per line.
let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]

var oddNumbers = luckyNumbers.filter { (luckyNumber: Int) -> Bool in
    if luckyNumber.isMultiple(of: 2)    {
        return false
    }
    return true
}

var oddLuckyNumbersSorted = oddNumbers.sorted(by: { (luckyNumber1, luckyNumber2) -> Bool in
    luckyNumber1 < luckyNumber2
})

print(oddLuckyNumbersSorted)

var mapLuckyNumbers: [()] = oddLuckyNumbersSorted.map({luckyNumber in

    print("\(luckyNumber) is a lucky number")
})

Whenever I finish doing a checkpoint, I check out the hints that Paul leaves for us and I am failing to understand what he means by

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

Can anyone point me in the right direction? I checked the lectures but couldn't pin point what he meant by this. I would also appreciate any help regarding the way my code is structured.

Thanks!

AM

2      

Look at the data types you have and the functions you are calling.

luckyNumbers is an array of Int. You call filter on it, which returns an array of Int you've assigned to oddNumbers.

oddNumbers is an array Int. You call sorted on it, which returns an array of Int you've assigned to oddLuckyNumbersSorted.

oddLuckyNumbersSorted is an array of Int. You call map on it, which returns an array of () you've assigned to mapLuckyNumbers.

Notice a pattern here?

So what Paul's hint is saying is that you can chain together methods depending on the types involved. You can eliminate the middlemen in your code and go from luckyNumbers all the way through to the end without needing all those intermediate variables.

You can think of it like a pipeline where, in this particular case, you start with an array of Int, call a bunch of methods on that array and the arrays returned by each step, to produce an array of () at the end. The output of each step becomes the input of the next step.

Does that make sense?

2      

Hi @roosterboy

Thank you for taking the time to answer my question. I felt that my code was redundent as I was writing it. So I can just declare a variable that holds an array of strings instead of three arrays.

Thanks for the clarification.

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your 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.