O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Functions as Arguments

Functions can serve as arguments to other functions. Recall, for example, that you initially gave sorted(by:) the sortAscending(_:_:) 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

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

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

func makeTownGrand() -> (Int, Int) -> Int {
    func buildRoads(byAddingLights ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required