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

  checkpoint 5: am I filtering out even numbers correctly (Swifty)?

Forums > 100 Days of SwiftUI

let arr = luckyNumbers.filter { !$0.isMultiple(of: 2)}.sorted().map{ "\($0) is a lucky number" }
for i in arr {
    print(i)
}

   

You can also write like this:

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

numbers.filter { $0 % 2 == 0 }
    .sorted()
    .forEach { print("\($0) is a lucky number") }

   

Which syntax is modern and easier to read comprehend?

{ !$0.isMultiple(of: 2) }

// or 

{ $0 % 2 == 0 }

Check this post from last year!

See -> Modulo vs isMultiple(of:)

   

Thanks @Obelix,

This is the AI answer on your question:

Both snippets of code you've provided represent closures in Swift, a programming language commonly used for developing iOS and macOS applications. These closures are used to determine if a number is even. However, it seems there's a bit of a mix-up in the description of what they do:

  1. { !$0.isMultiple(of: 2) } – This closure checks if a number is not a multiple of 2, meaning it's used to identify odd numbers. The ! operator negates the result of isMultiple(of: 2), which checks for even numbers.

  2. { $0 % 2 == 0 } – This closure directly checks if a number is even by using modulo arithmetic. If a number divided by 2 has a remainder of 0, it's an even number.

To compare the syntax and ease of comprehension:

  • The { !$0.isMultiple(of: 2) } uses the isMultiple(of:) method which is more modern and introduced in more recent versions of Swift. It directly expresses the intention (checking for multiples) and is less prone to errors compared to traditional modulo checking. However, using ! to negate it adds a slight cognitive load as it inverts the logic.
  • The { $0 % 2 == 0 } uses traditional modulo arithmetic which is very common and widely understood in many programming languages. This might make it easier to understand for programmers coming from other languages or those accustomed to this common idiom for checking even numbers.

In summary, if you're working in Swift and your audience is familiar with Swift's modern features, { !$0.isMultiple(of: 2) } can be considered a cleaner and more explicit approach in terms of expressing "not a multiple of 2" despite the negation. For general comprehensibility, especially for those familiar with different programming languages, { $0 % 2 == 0 } remains a very straightforward and clear approach for checking if a number is even.

   

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.