Collection

extension Collection
extension Collection where Element: Additive
extension Collection where Element: Multiplicative
extension Collection where Element: AdditiveSemigroup
extension Collection where Element: MultiplicativeSemigroup
  • let numbers = [1,2,3]
    let accumulatedAdditionFrom10 = numbers.accumulating(10,+) // => [10,11,13]
    

    Declaration

    Swift

    public func accumulating(
        _ initial: Element,
        _ op: (Element,Element) -> Element
    ) -> [Element]

    Return Value

    An array of values starting with the given initial, followed by each element combined with the accumulation with the given op.

Available where Element: Additive

  • Declaration

    Swift

    public var accumulatingSum: [Element] { get }

    Return Value

    An array of values from .zero, applying + to each element contained herein and the accumulative value.

    let numbers = [2,1,2] let accumulated = numbers.accumulatingSum // => [0,2,3]

Available where Element: Multiplicative

  • let numbers = [2,1,2]
    let accumulated = numbers.accumulatingProduct // => [1,2,2]
    

    Declaration

    Swift

    public var accumulatingProduct: [Element] { get }

    Return Value

    An array of values from .one, applying * to each element contained herein and the accumulative value.

Available where Element: AdditiveSemigroup

  • Declaration

    Swift

    public var nonEmptySum: Element? { get }

    Return Value

    The sum of the elements contained herein, if !self.isEmpty. Otherwise, nil.

Available where Element: MultiplicativeSemigroup

  • Declaration

    Swift

    public var nonEmptyProduct: Element? { get }

    Return Value

    The product of the elements contained herein, if !self.isEmpty. Otherwise, nil.