Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 2: Closures

18. Challenge: filter & reduce

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 17. filter, reduce, & sort Next episode: 19. Conclusion

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

It’s here! The final challenge on closures. You’ll find it on page 10 of the Playground for this part of the course. Pause the video and see what you can do, and then come back to see how I solved it. Have fun!

let names = ["Ray", "Vicki", "Ozma", "Catie", "Jessy"]
let allNames = names.reduce
let allNames = names.reduce("") { 
}
let allNames = names.reduce("") { result, name in

}
let allNames = names.reduce("") { result, name in
  result + " " + name
}
let filteredNames = names.filter { $0.count > 4
}
.reduce("") { result, name in
  return result + " " + name
}
let namesAndAges = ["Ozma": 7, "Leo": 14, "Jessy": 35, "Albus": 150]
let under18 = namesAndAges.filter { $0.value < 18 }
let under18 = namesAndAges.filter { 
  $0.value < 18 
}
let adults = namesAndAges.filter { 
  $0.value >= 18
}
.map { nameAndAge in
  nameAndAge.key
}

adults