Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 2: Closures

18. Challenge: filter & reduce

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

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

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 18. Challenge: filter & reduce

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’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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