Leave a rating/review
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.
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!
Ready to go over the challenge? First, I’ll create an array with five names in it. Ray, Vicki, Ozma, Catie, and Jessy.
let names = ["Ray", "Vicki", "Ozma", "Catie", "Jessy"]
I’ll save the result of the reduce function in a constant called “allNames”.
let allNames = names.reduce
For the parameters, I’ll pass in an empty string to start with, and then use trailing closure syntax for the closure.
let allNames = names.reduce("") {
}
Inside the closure, I’ll the two parameters “result” and “name”, and just ignore the return type
let allNames = names.reduce("") { result, name in
}
And finally I’ll return the result plus a space plus the name.
let allNames = names.reduce("") { result, name in
result + " " + name
}
Over on the right, you can see the result is a single string, with space between each name! For the next one, I’ll start by filtering the names array, and making sure each name has more than four characters.
let filteredNames = names.filter { $0.count > 4
}
I used the shortened syntax, there. To finish it up, I can just copy and paste the reduce code to the end of the closure!
.reduce("") { result, name in
return result + " " + name
}
Next challenge! I’ll create a dictionary called “namesAndAges”, as prescribed. Ozma is 7, Leo is 14, Jessy is 35, and Albus is 150.
let namesAndAges = ["Ozma": 7, "Leo": 14, "Jessy": 35, "Albus": 150]
Then I can use filter to check if the value of each pair is less than 18.
let under18 = namesAndAges.filter { $0.value < 18 }
You may have noticed, but if you write your closure all on one line, the results don’t show up in the sidebar as you might expect. So, I’ll space this out.
let under18 = namesAndAges.filter {
$0.value < 18
}
It turns out only Ozma and Leo are under 18. I used more short syntax! If you wrote it longer, that’s fine too! In fact, again, I encourage you to practice writing closures in all sorts of syntaxes!
Last challenge. I can reuse that dictionary, and, again, I copy the code!
But this time I’ll change the constant to be “adults” and return the pairs with values greater than or equal to 18.
let adults = namesAndAges.filter {
$0.value >= 18
}
To finish it off, I’ll use map. This time I’ll name the parameter, for a change of pace, and then return just the key for each parameter.
.map { nameAndAge in
nameAndAge.key
}
adults
Now I’ve got an array of just the names of the adults from the dictionary! Note that I put the dot map on a new line, but the functions still chain correctly!
This is another stylistic decision you can make. Think about what’s easy for you to read, and observe how other people format their code. As always, it’s up to you to decide in the end!