From the course: Learning Higher-Order Functions with Swift

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Chaining higher-order functions

Chaining higher-order functions

From the course: Learning Higher-Order Functions with Swift

Start my 1-month free trial

Chaining higher-order functions

- [Instructor] So another great optimization or powerful use of higher order functions is chaining. Higher order functions return something, right? Most of them do. Obviously sort doesn't, but many, many of them do. And they often return things like collections, things that you were calling them on to begin with. So you could chain functions together and call other higher-order functions on what was returned. So let's look at doing that. So once again I'm going to delete all my code other than my array of names and I'm going to look for the first name that has the letter E when they've been sorted. So I'm going to need two higher order functions for that. So I'll say let first E equal names dot sort I'm going to called sorted because I want them returned. And then I'm going to say first, where. V1 I'll say. And I'll return v1 contains E, and again I'll call lower-cased on v1. Now we know from our optimizations we can do better than this but let's go with this for now. Let's see if…

Contents