Array

public extension Array
public extension Array where Element: Consolidatable

Closure Type Aliases

  • Closure used to consolidate two Element values.

    Declaration

    Swift

    typealias Consolidate = (_ first: Element, _ second: Element) -> Element
  • Closure used to decide whether two Element values should be consolidated.

    Declaration

    Swift

    typealias ShouldConsolidate = (Element, Element) -> Bool

Consolidation

  • Consolidates (reduces) an array of Elements by a KeyPath using 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 TaxAmount type is consolidated by name, the two entries for “Sales Tax” have been consolidated into a single TaxAmount where their amount values have been added.

    Declaration

    Swift

    @inlinable
    func consolidated<GroupType>(by keyPath: KeyPath<Element, GroupType>, using consolidating: Consolidate) -> Array<Element> where GroupType : Hashable

    Parameters

    keyPath

    The key path to the property to use as a consolidation group.

    consolidating

    The closure used to consolidate two Element values into a single Element value.

    Return Value

    A new array of elements that have been consolidated by a KeyPath using the given consolidating closure.

  • Consolidates (reduces) an array of Elements, by a KeyPath using 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 TaxAmount type is consolidated by name, the two entries for “Import Tax” have been consolidated into a single TaxAmount where their amount values 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 TaxAmount entries would consolidate to two elements (Import Tax and Sales Tax) the example above would throw the error ConsolidationError.couldNotBeConolidatedIntoSingleElement.

    Throws

    ConsolidationError.couldNotBeConolidatedIntoSingleElement error 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 : Hashable

    Parameters

    keyPath

    The key path to the property to use as a consolidation group.

    consolidating

    The closure used to consolidate two Element values into a single Element value.

    Return Value

    A single element representing the consolidation by a KeyPath using the given consolidating closure.

  • Consolidates (reduces) an array of Elements grouped 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 TaxAmount type is consolidated by name, the two entries for “Sales Tax” have been consolidated into a single TaxAmount where their amount values have been added.

    Declaration

    Swift

    @inlinable
    func consolidated(by comparison: ShouldConsolidate, using consolidating: Consolidate) -> Array<Element>

    Parameters

    comparison

    The closure used to decide whether two Element values should be consolidated.

    consolidating

    The closure used to consolidate two Element values into a single Element value.

    Return Value

    A new array of elements that have been grouped by the result of a closure combined using another closure.

Errors

Where Element type implements Consolidatable

  • Consolidates (reduces) an array of Elements that implement the Consolidatable protocol.

    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 TaxAmount type is consolidated by name, the two entries for “Sales Tax” have been consolidated into a single TaxAmount where their amount values 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 of consolidate(with:)