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

SOLVED(?) Checkpoint 5. Feedback desired lol

Forums > 100 Days of SwiftUI

@xan  

Just made my way through this: https://www.hackingwithswift.com/quick-start/beginners/checkpoint-5

Here's what I came up with, not sure if it's entirely correct but seems to get the desired result?

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

func playLottery(yourNumbers: [Int]) -> Void {
    let drawnNumbers = yourNumbers.filter {
        !$0.isMultiple(of: 2)
    }
    .sorted()
    .map {
        print("\($0) is a lucky number")
    }
}

playLottery(yourNumbers: luckyNumbers)

I feel like it reads clean enough and prints out the desired result, is there anything wrong with it I should clean up?

Trying to cleanse my mind of doing a decade of PHP and hope it's not infecting the way I'm approaching problems. =P

Edit: Actually, please also point out how this should normally be formatted with Swifts typical style. I'm used to stacking calls like that in PHP but haven't seen if that's entirely how you'd line things up normally when writing it. It seemed to be what XCode wanted to do, which is also what I'd normally have done but just thought I'd double check.

2      

@xan is moving at a quick pace and asks for feedback:

Actually, please also point out how this should normally be formatted with Swifts typical style.

You're ahead of the curve, since you already know the basics of programming structure, logic, functions, etc. Nice! You're focusing your attention on more advanced skills that you'd use in a team environment: program structure, style guides, etc.

You're on a good path! Perhaps don't worry so much until later lessons. Follow @twoStraw's videos and make mental notes on formatting, etc. Also, you'll pick up good hints in other online tutorials (Karin Prather, Sean Allen, CodeWithChris, CS193p, Rebeloper, Swiftful Thinking, etc). And of course, you've found that out-of-the-box XCode tries to help you with formatting too.

Because you already have a decent programming foundation, I'd offer this small comment. Swift was designed to take much of the arcane syntax out (looking at you semi-colons!) allowing much more natural reading syntax. Take advantage of this, and review your own code for clarity.

If you and I were having a conversation about playing the lottery, which sounds more like a conversation?

First try

Obelix: Hey! Lottery is approach 1/2 billion dollars again!
@xan: We should pool our lucky numbers and buy some tix.
Obelix: Splendid!
@xan: playLottery(yourNumbers: luckyNumbers). <-- Can this sound more natural?

Second try

Obelix: Splendid!
@xan: playLottery(with: myLuckyNumbers). <-- Maybe?

This is an observation about style. So, there is no correct answer. But in a peer review you might get this type of feedback.

Keep coding!

2      

Another round of style comments.

Often you'll see computed properties, or trailing closures that contain a single line of code. Your example has two:

let drawnNumbers = yourNumbers.filter {
        !$0.isMultiple(of: 2)      //. <-- Single line in this closure
    }
    .sorted()
    .map {
        print("\($0) is a lucky number")  //. <-- Single line in this closure
    }

@twoStraws (and others) often condense these to a single line.
First, it makes it clear that you're providing a function. Second, well? It's a nice way to compact your code.

// Condense single line closures to a single line.
// Also consider aligning the periods when chaining functions together.
let drawnNumbers = yourNumbers.filter {  !$0.isMultiple(of: 2) }
                              .sorted()
                              .map { print("\($0) is a lucky number") }

Also, notice how the periods are aligned? This enforces the concept that you've chained three functions together.

Again, this is style preferences. So there are no wrong answers. Just very wrong answers.

3      

@xan  

Ohhh duh that makes perfect sense. Still getting into the habit of one lining things. That does look way better!

2      

Some additional notes:

1.Since your playLottery function doesn't return a value, you have indicated the return type is Void. That's fine but the usual Swift style is to just not indicate any return type at all. So:

func playLottery(yourNumbers: [Int]) {
   ...
}
  1. Method chaining is great and perfectly Swifty. Since you aren't returning anything from your playLottery function, however, it's better to use forEach instead of map. map is for transforming a collection of values of one type into a collection of values of another type. So, like [Int] to [String]. You're not doing that here, you're just looping through a collection and printing out its values.

You also don't need the drawnNumbers declaration. because you aren't doing anything with it. The playLottery function doesn't return a value and you do no further operations on drawnNumbers.

So your method becomes:

func playLottery(yourNumbers: [Int]) {
    yourNumbers.filter { !$0.isMultiple(of: 2) }
               .sorted()
               .forEach { print("\($0) is a lucky number") }
}

By using the print function inside map, you actually end up with an array of void functions, i.e., [()], because print doesn't return any value.

However, Paul's instructions actually say to map the odd numbers to the string "\($0) is a lucky number" and then print the resulting array, one item per line. That would look a little more like this:

func playLottery(yourNumbers: [Int]) {
    yourNumbers.filter { !$0.isMultiple(of: 2) }
               .sorted()
               .map { "\($0) is a lucky number" }
               .forEach { print($0) }
}

But that seems like an unnecessary extra step to me, since you can just use a forEach and print out the value directly after sorted rather that mapping to an intermediate array (that's an extra loop).

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.