Functions as Arguments

Functions can serve as arguments to other functions. Recall, for example, that you initially gave sort(_:) the sortAscending(_:j:) function as an argument.

Practicality suggests that your town can only build roads when it has a suitable budget. Adjust your previous makeTownGrand() function to take a budget parameter and a condition parameter. The budget parameter will serve as your town’s budget, and the condition parameter will evaluate whether this budget is suitable to build the new roads.

Listing 13.9  Adding budget considerations

import Cocoa

var volunteerCounts = [1,3,40,32,2,53,77,13]

let volunteersSorted = volunteerCounts.sort { $0 < $1 }

func makeTownGrand() -> (Int, Int) -> Int {
    func buildRoads(lightsToAdd: ...

Get Swift Programming: The Big Nerd Ranch Guide 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.