1

I've learned so far that the reduce() function in Swift is used to produce a single value from elements derived from an entire sequence. However, to my understanding the reduce() function requires two arguments to be called, the initialResult which is to be used in conjunction the second argument the nextPartialResult. An example implementation from the Swift documentation is below:

let numbers = [1, 2, 3, 4]
let numberSum = numbers.reduce(0, { x, y in
    x + y
})
// numberSum == 10

However, in certain implementations of reduce() I have seen code such as the following:

let totalIncome = peopleArray.reduce(0) {(result, next) -> Double in
    return result + next.income
}

print("Total Income: \(totalIncome) Average Income: \(totalIncome/Double(peopleArray.count))")
//Result: Total Income: 450000.0 Average Income: 150000.0

Code courtesy of codeburst

I am not sure whether this is a syntactical variation of writing the same thing (i.e. if somehow two arguments are indeed being passed into the reduce() function) or whether or not an argument is being omitted and if so, how that might affect the use cases of the function.

4
  • What do you think the {(result, next) -> Double in return result + next.income } part is? Commented Sep 13, 2020 at 4:01
  • Swift is a type inferred language. Most of the times you don't need to specify the resulting type -> Double. Btw the parentheses are not required. result, next in Commented Sep 13, 2020 at 4:05
  • @Sweeper Part of my question was asking if it is a syntactical variation in writing the same thing--based on my current (and fairly rudimentary) knowledge of Swift functions so far, arguments are enclosed parenthetically in the function's declaration and so the closure didn't appear to be listed as an argument. Commented Sep 13, 2020 at 4:07
  • 1
    func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Int) throws -> Result) rethrows -> Result The first argument is the initial result 0 the second argument is the nextPartialResult a method which is being passed using a syntax called trailing closure (that's why it is after the closing parentheses). As I have already mentioned the type inferred Result which is a Double in your second example is not needed in most cases. Commented Sep 13, 2020 at 4:11

1 Answer 1

2

What you are seeing is a trailing closure.

You write a trailing closure after the function call’s parentheses, even though the trailing closure is still an argument to the function. When you use the trailing closure syntax, you don’t write the argument label for the first closure as part of the function call.

(emphasis mine)

Starting from Swift 5.3, you can write multiple trailing closures for a function call. Before that only the last argument, that is also a closure, can be a trailing closure.

So the reduce call is equivalent to:

let totalIncome = peopleArray.reduce(0, {(result, next) -> Double in
    return result + next.income
})

I am not sure whether this is a syntactical variation of writing the same thing (i.e. if somehow two arguments are indeed being passed into the reduce() function)

Yes, this is just a syntactical variation, or "syntactic sugar" as we'd like to call it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.