Documentation Language: Swift

Function

iterate(_:_:_:)

Accumulates elements of a collection using the given accumulator function.

func iterate(_ collection: any EcoreValue, _ initial: any EcoreValue, _ accumulator: (any EcoreValue, any EcoreValue) throws -> any EcoreValue) throws -> any EcoreValue

Parameters

collection

The collection to iterate over

initial

The initial value for the accumulation

accumulator

A closure that takes the current accumulated value and an element

Return Value

The final accumulated value

Discussion

The iterate operation performs a fold/reduce operation, starting with an initial value and applying the accumulator function to each element in sequence.

Examples

let numbers = [1, 2, 3, 4] as [EInt]
let sum = try iterate(numbers, 0 as EInt) { acc, elem in
    (acc as? EInt ?? 0) + (elem as? EInt ?? 0)
}
// Result: 10