From the course: Learning Higher-Order Functions with Swift
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Higher-order functions on strings
From the course: Learning Higher-Order Functions with Swift
Higher-order functions on strings
- [Instructor] The last one we're going to talk about, I know, it's sad, I didn't want it to end. I've enjoyed this. But the last one we're going to talk about is calling higher-order functions on strings. So once again I'm going to delete all the code in my playground, good-bye names, and I'm going to create a new value and I'm going to call it, just name. Just one and it's going to be a very long name, something like Jenicatimobriathan, something like that. Okay, well since strings in Swift are collections, you can call higher-order functions on them, no problem. So we can say let totalChars = name.reduce. And again, what's our initial value when we're going to add up the characters? Zero, so the next result is going to be ongoing and it's also an Int. The next one is our char and the result is an Int. Well this isn't very interesting 'cause I'm just always going to return one, but I have to add it to ongoing and it's 18. That's not very interesting, we could get that by just doing…
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.