Array
public extension Array
public extension Array where Element: Consolidatable
-
Closure used to consolidate two
Elementvalues.Declaration
Swift
typealias Consolidate = (_ first: Element, _ second: Element) -> Element -
Closure used to decide whether two
Elementvalues should be consolidated.Declaration
Swift
typealias ShouldConsolidate = (Element, Element) -> Bool
-
Consolidates (reduces) an array of
Elementsby aKeyPathusing a given closure.Example
let allTaxes = [ TaxAmount(name: "Import Tax", amount: 3.00), TaxAmount(name: "Sales Tax", amount: 1.75), TaxAmount(name: "Import Tax", amount: 2.30) ] let consolidatedTaxes = allTaxes.consolidated(by: \.name) { TaxAmount(tax: $0.name, amount: $0.amount + $1.amount) } // Would result in: let consolidatedTaxes = [ TaxAmount(name: "Import Tax", amount: 5.30), TaxAmount(name: "Sales Tax", amount: 4.10) ]Since the
TaxAmounttype is consolidated by name, the two entries for “Sales Tax” have been consolidated into a singleTaxAmountwhere theiramountvalues have been added.Declaration
Swift
@inlinable func consolidated<GroupType>(by keyPath: KeyPath<Element, GroupType>, using consolidating: Consolidate) -> Array<Element> where GroupType : HashableParameters
keyPathThe key path to the property to use as a consolidation group.
consolidatingThe closure used to consolidate two
Elementvalues into a singleElementvalue.Return Value
A new array of elements that have been consolidated by a
KeyPathusing the givenconsolidatingclosure. -
Consolidates (reduces) an array of
Elements, by aKeyPathusing a given closure, into a single element.Example
let allTaxes = [ TaxAmount(name: "Import Tax", amount: 3.00), TaxAmount(name: "Import Tax", amount: 2.30) ] // The next line would result in TaxAmount(name: "Import Tax", amount: 5.30) let consolidatedTaxes = allTaxes.consolidated(by: \.name) { TaxAmount(tax: $0.name, amount: $0.amount + $1.amount) }Since the
TaxAmounttype is consolidated by name, the two entries for “Import Tax” have been consolidated into a singleTaxAmountwhere theiramountvalues have been added.let allTaxes = [ TaxAmount(name: "Import Tax", amount: 3.00), TaxAmount(name: "Sales Tax", amount: 1.75), TaxAmount(name: "Import Tax", amount: 2.30) ] // The next line would throw let consolidatedTaxes = allTaxes.consolidated(by: \.name) { TaxAmount(tax: $0.name, amount: $0.amount + $1.amount) }Since the
TaxAmountentries would consolidate to two elements (Import Tax and Sales Tax) the example above would throw the errorConsolidationError.couldNotBeConolidatedIntoSingleElement.Throws
ConsolidationError.couldNotBeConolidatedIntoSingleElementerror if the array does not consolidate into a single element.Declaration
Swift
@inlinable func consolidatedIntoSingle<GroupType>(by keyPath: KeyPath<Element, GroupType>, using consolidating: Consolidate) throws -> Element where GroupType : HashableParameters
keyPathThe key path to the property to use as a consolidation group.
consolidatingThe closure used to consolidate two
Elementvalues into a singleElementvalue.Return Value
A single element representing the consolidation by a
KeyPathusing the givenconsolidatingclosure. -
Consolidates (reduces) an array of
Elementsgrouped by the result of a closure combined using another closure.Example
let allTaxes = [ TaxAmount(name: "Import Tax", amount: 3.00), TaxAmount(name: "Sales Tax", amount: 1.75), TaxAmount(name: "Import Tax", amount: 2.30) ] let consolidatedTaxes = allTaxes.consolidated(by: { $0.name == $1.name }) { TaxAmount(tax: $0.name, amount: $0.amount + $1.amount) } // Would result in: let consolidatedTaxes = [ TaxAmount(name: "Import Tax", amount: 5.30), TaxAmount(name: "Sales Tax", amount: 4.10) ]Since the
TaxAmounttype is consolidated by name, the two entries for “Sales Tax” have been consolidated into a singleTaxAmountwhere theiramountvalues have been added.Declaration
Swift
@inlinable func consolidated(by comparison: ShouldConsolidate, using consolidating: Consolidate) -> Array<Element>Parameters
comparisonThe closure used to decide whether two
Elementvalues should be consolidated.consolidatingThe closure used to consolidate two
Elementvalues into a singleElementvalue.Return Value
A new array of elements that have been grouped by the result of a closure combined using another closure.
-
Errors that can occur during consolidation
See moreDeclaration
Swift
enum ConsolidationError : Error
-
Consolidates (reduces) an array of
Elementsthat implement theConsolidatableprotocol.Example
struct TaxAmount: Consolidatable { let name: String let amount: Decimal var consolidationGroup: AnyHashable { return name } func consolidate(with other: Self) -> Self { return .init(name: name, amount: amount + other.amount) } }The above implementation would consolidate like this:
let taxes = [ TaxAmount(name: "Import Tax", amount: 3.00), TaxAmount(name: "Sales Tax", amount: 1.75), TaxAmount(name: "Import Tax", amount: 2.30) ].consolidated() // Results in: let taxes = [ TaxAmount(name: "Import Tax", amount: 5.30), TaxAmount(name: "Sales Tax", amount: 4.10) ]Since the
TaxAmounttype is consolidated by name, the two entries for “Sales Tax” have been consolidated into a singleTaxAmountwhere theiramountvalues have been added.Declaration
Swift
@inlinable func consolidated() -> Array<Element>Return Value
A new array of elements that have been consolidated by group (
consolidationGroup) using the implementation ofconsolidate(with:)
View on GitHub
Array Extension Reference