Let's assume I want to use solve for another array. Could I achieve this by changing luckyNumbers.filter to just .filter ?
Not quite. You did have one bug in your code.
You wrote:
let solve = { (_: [Int]) in // this requires an array of [Int], but you never use this array in your solution.
luckyNumbers.filter { !$0.isMultiple(of: 2) } // Here you HARD coded the array you wanted to process
// you are NOT using the array that you passed into this code.
// By using the _ character, you're telling Swift to ignore what is passed in.
Run this in your playground:
solve(luckyNumbers)
print("======= Another array of ints ========"
solve( [ 9, 99, 199, 999, 42] )
Both will provide the same result! Why? Because you HARD coded the array name into your filter statement. In the second line, your function accepts the array of [ 9, 99, 199, 999, 42], but ignores it.
To analyze different arrays of ints, you'll need to change your function's signature.
let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]
let moreNumbers = [7, 1, 14, 13, 42, 199, 24]
let solve = { (numbers: [Int]) in // Change your input, name it numbers. Any array of int will be called numbers in your function.
numbers.filter { !$0.isMultiple(of: 2) } // numbers is now filtered, sorted, mapped.
.sorted()
.map { print("\($0) is a lucky number.") }
}
print("Lucky Number ===========")
solve(luckyNumbers)
print("More Numbers ===========")
solve(moreNumbers)