In a short way:

let reduced = array.reduce(0) { $1.isAvailable ? 1.bars.count : $0 }

Now, let's explicit it:

The logic is quite simple:

  • We set the initial value to 0
  • In the closure, we have two parameters, the first one is the current value (at start it's the initial value), which we'll increment at each iteration, and the second one is the element of the array. We return then the new value (partial) that we incremented or not according to your condition (here is isAvailable)

Explicitly:

let reduced2 = array.reduce(0) { partial, current in
    if current.isAvailable {
        return partial + current.bars.count
    } else {
        return partial
    }
}

With a ternary if:

let reduced3 = array.reduce(0) { partial, current in
    return current.isAvailable ? partial + current.bars.count : partial
}

Getting rid of the return, see Functions With an Implicit Return in Functions of Swift or Implicit Returns from Single-Expression Closures of Closures of Swift.

let reduced4 = array.reduce(0) { partial, current in
    current.isAvailable ? partial + current.bars.count : partial
}

With Shorthand Argument Names on Closures of Swift.

let reduced5 = array.reduce(0) { $1.isAvailable ? 1.bars.count : $0 }
Answer from Larme on Stack Overflow
🌐
SwiftLee
avanderlee.com › swiftlee › swift › swift reduce: combining elements into a single value
Swift Reduce: Combining elements into a single value - SwiftLee
April 15, 2025 - Learn how to use the swift reduce method to convert collections into single values like arrays into dictionaries.
Discussions

arrays - Using Reduce to sum (Swift) - Stack Overflow
Im looking through examples were you can use reduce to sum an array, but mostly finding examples with numbers. How would you add up all Bar that are inside Foo that has isAvailable set to true using More on stackoverflow.com
🌐 stackoverflow.com
Adding an index with functional methods like map, reduce, filter - Pitches - Swift Forums
Here's a quick draft pitch before I can expand on it. The gist is to add an index to the functional methods, so it can allow for something like (note mapi is used to differentiate it from map) let arrayNumbers = [1, 2, … More on forums.swift.org
🌐 forums.swift.org
0
June 17, 2018
Swift Do Something Useful with Zip, Map, and Reduce

A question about this:

What's the difference between applying a mapping function on an array vs doing .forEach?

More on reddit.com
🌐 r/swift
6
13
February 9, 2014
I need to compress an image without losing too much quality
hey, i asked this question! :D if you scroll down the answers a bit, user4261201 proposes a different solution. I would recommend trying that and seeing if it works, to be honest i think i still use the answer provided by him. More on reddit.com
🌐 r/swift
5
5
August 3, 2016
🌐
Swdevnotes
swdevnotes.com › swift › 2022 › reduce-in-swift
Reduce in Swift | Software Development Notes
June 4, 2022 - 1let total2 = nums.reduce(0) { x, y in x + y } This can be shortened further, using the shorthand argument names provided automatically by Swift to inline closures. The trailing closure is updated to simply add the first parameter to the second parameter. 1let total3 = nums.reduce(0) { $0 + $1 } Finally, this can be shortened even further, by eliminating the closure and specifying the operation as the second parameter to the reduce function.
Top answer
1 of 3
5

In a short way:

let reduced = array.reduce(0) { $1.isAvailable ? 1.bars.count : $0 }

Now, let's explicit it:

The logic is quite simple:

  • We set the initial value to 0
  • In the closure, we have two parameters, the first one is the current value (at start it's the initial value), which we'll increment at each iteration, and the second one is the element of the array. We return then the new value (partial) that we incremented or not according to your condition (here is isAvailable)

Explicitly:

let reduced2 = array.reduce(0) { partial, current in
    if current.isAvailable {
        return partial + current.bars.count
    } else {
        return partial
    }
}

With a ternary if:

let reduced3 = array.reduce(0) { partial, current in
    return current.isAvailable ? partial + current.bars.count : partial
}

Getting rid of the return, see Functions With an Implicit Return in Functions of Swift or Implicit Returns from Single-Expression Closures of Closures of Swift.

let reduced4 = array.reduce(0) { partial, current in
    current.isAvailable ? partial + current.bars.count : partial
}

With Shorthand Argument Names on Closures of Swift.

let reduced5 = array.reduce(0) { $1.isAvailable ? 1.bars.count : $0 }
2 of 3
2

You can achieve this using a single reduce operation. Start from a 0 result, then in the closure, check foo.isAvailable and only increment the count if it is true.

let totalCount = array.reduce(0, { accumulatingResult, foo in
  guard foo.isAvailable else { return accumulatingResult }
  return accumulatingResult + foo.bars.count
})
🌐
DEV Community
dev.to › tgarayua › mastering-the-reduce-method-in-swift-690
Mastering the .reduce() Method in Swift - DEV Community
October 28, 2023 - The .reduce() method in Swift is a powerful tool for aggregating data in arrays, making it an indispensable feature for any Swift developer. Whether you need to calculate sums, find maximum values, or perform custom operations, understanding ...
Find elsewhere
🌐
Medium
juannavas7.medium.com › exploring-reduce-in-swift-ead6b707b455
Exploring how to use reduce in Swift | by Juan Navas | Medium
November 26, 2020 - Basically reduce iterates over a collection, taking 2 arguments, an initial value and a closure which applies a transformation to that value. This closure provides two parameters, the partial result obtained in each iteration, and the next element ...
🌐
Polpiella
polpiella.dev › mastering-the-reduce-operator-in-swift
Master the reduce operator in Swift and make your code more performant
April 3, 2024 - Swift’s Sequence type has a powerful operator called reduce which allows you to combine all elements of a sequence into a single value.
🌐
Medium
medium.com › @iamCoder › swift-reduce-explained-a93831ad9042
Swift: reduce(_:_:) explained. The reduce function is used to combine… | by Adam Dev | Medium
June 29, 2025 - The reduce function is used to combine all elements of a collection (like an array) into a single value.
🌐
Codecademy
codecademy.com › docs › swift › arrays › .reduce()
Swift | Arrays | .reduce() | Codecademy
October 24, 2022 - The .reduce() method loops or iterates over every item in a sequence, combines them into one value using a specified closure, and returns the combined result. ... Learn how to build iOS applications with Swift and SwiftUI and publish them to ...
🌐
Bugfender
bugfender.com › blog › swift-arrays
Swift Arrays: Map, Filter, Reduce & Sort Explained | Bugfender
November 7, 2025 - As we said at the top, the whole point of Swift arrays is to order our elements. We need a clear, consistent way of doing this. Actually there are two principal methods we can use for sorting: ... To simplify and reduce repetition, we’ll use sorted in our examples from here on.
🌐
Use Your Loaf
useyourloaf.com › blog › swift-guide-to-map-filter-reduce
Swift Guide to Map Filter Reduce
May 1, 2018 - Using a for loop to work with collections? Take a few minutes to learn about Swift map, filter and reduce operations.
🌐
DhiWise
dhiwise.com › post › swift-reduce-streamlining-swift-arrays-for-enhanced-efficiency
A Comprehensive Guide to Implementing Swift Reduce
November 12, 2024 - Swift reduce is a powerful higher-order function used for operations like filtering, transforming, and combining elements in an array efficiently. As one of the higher-order functions in Swift, reduce takes a function as an input and returns ...
🌐
Brightec
brightec.co.uk › home › blog › how to filter, map, and reduce in swift
Swift Basics: Mapping, Filtering, and Reducing in Swift |… | Brightec
August 16, 2023 - Swift’s map, filter, and reduce functions are often referred to as higher-order functions. This is because they take a function as an input and return it as an output. Using map, filter, and reduce in Swift results in clean, concise code that is more readable and offers better results.
🌐
Swiftly
swiftly.dev › reduce
reduce • Swift 5.7 references for busy coders | Swiftly
let numbers = [1, 2, 3, 4] let numbersSum = numbers.reduce(0) { $0 + $1 } let numbersProduct = numbers.reduce(1) { $0 * $1 } print(numbersSum) // 10 print(numbersProduct) // 24
🌐
Objc
talk.objc.io › episodes › S01E150-the-origins-of-reduce
The Origins of Reduce - Swift Talk - objc.io
May 3, 2019 - 05:55 But where does reduce come from? We can explore its origins by defining a singly linked list and finding a reduce operation on it. In Swift, we define a linked list as an enum with a case for an empty list and a case for a non-empty list. The non-empty case is traditionally called cons, and its associated values are a single list element and a tail.
🌐
Swift Forums
forums.swift.org › evolution › pitches
Adding an index with functional methods like map, reduce, filter - Pitches - Swift Forums
June 17, 2018 - Here's a quick draft pitch before I can expand on it. The gist is to add an index to the functional methods, so it can allow for something like (note mapi is used to differentiate it from map) let arrayNumbers = [1, 2, 3, 4, 5, 6] let oddOnes = arrayNumbers.mapi{(number, index) in if index % 2 == 0 { return number } else { return number * 2 } } in case of reduce, filter etc it can be used in a similar fashion where the index helps to eliminate loops that would ot...
🌐
Medium
medium.com › @armanabkar › map-filter-and-reduce-in-swift-e97558c21c89
Map, Filter, and Reduce in Swift. Higher-Order Functions such as map… | by Arman | Medium
January 5, 2022 - let nums = [2.0, 4.0, 5.0] var total = nums.reduce(10.0) { (result, el) in return result + el // 21.0 } total = nums.reduce(10.0, +) // 21.0 - simplified using + These are variations on the map method that flatten or compact the result. flatMap is used to map and then flatten a collection of collections: let nums = [[4, 3], [9, 6, 4], [2]] let allNums = nums.flatMap { $0 } // [4, 3, 9, 6, 4, 2] let certainNums = nums.flatMap { $0.filter { $0 > 5} } // [9, 6] In Swift 3, this was also used for removing nil values from the collection.
🌐
Hacking with Swift
hackingwithswift.com › example-code › language › how-to-sum-an-array-of-numbers-using-reduce
How to sum an array of numbers using reduce() - free Swift example code and tips
May 28, 2019 - Swift version: 5.10 · Paul Hudson @twostraws May 28th 2019 · The reduce() method is designed to convert a sequence into a single value, which makes it perfect for calculating the total of an array of numbers.