Functions as Arguments

You initially sorted your volunteerCounts array using a named function passed as an argument to sorted(by:). Although that particular task could be accomplished elegantly without the need for a named function, there are times when declaring a function and passing it as an argument to another function is the best solution.

Now you will write your own function that takes a closure to modify a collection. In the playground, add this incomplete function:

Example 13.7. Formatting numbers as strings
...
func format(numbers: [Double], using formatter: (Double) -> String) -> [String] {
    var result = [String]()

    return result
}

That is a lot of parentheses! Do not be intimidated. Study the function’s signature from beginning to ...

Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.