Hey guys,
I am currently on day 8 and I am a bit overwhelmed with closures.
In checkpoint 5, our job is to:
- Filter out any numbers that are even
- sort the array in ascending order
- map them to strings in the format "7 is a lucky number"
- 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
- 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