From the course: Learning Higher-Order Functions with Swift
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Higher-order functions on sets
From the course: Learning Higher-Order Functions with Swift
Higher-order functions on sets
- [Instructor] We want to talk real quickly on calling higher-order functions on other types of collections. We've been focusing on arrays. But let's talk about sets. Let's make our name a type of set that contains strings. So if we wanted to call one of these functions on this, what are we going to get back? So let's look. We'll say, names.sorted. And what does it come back? Well that's an array. That's right, I mean, what does sorted mean? It means ordered. Well, sets, by definition are unordered. So when you sort a set, it's actually going to turn back an array. So you have to be real careful about what you're doing when you call these higher-order functions on other types. Obviously, reduce is still going to be the same thing, but maybe not in the same order. Let's try it. So our initial result is going to be an empty string. We'll have our ongoing and then our string, which is going to be val, and then our result is a string. And we'll just return the ongoing plus val. Now how…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.