Learn how strings are collections as well. They are a collection of characters. As such, higher order functions can be called on them just like other collections. You can loop through the characters and do whatever processing, comparing, and converting necessary. The return type will vary based on the function called.
- [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 name.count.…We could upper lowercase every other item,…
- What are closures?
- Closures and function types
- Basic higher-order functions, including sort and forEach
- Powerful higher-order functions, including map and reduce
- Passing functions
- Closure optimizations
- Higher-order functions on sets, dictionaries, and strings