Documentation Language: Swift

Function

collect(_:_:)

Transforms each element of a collection using the given transform function.

func collect(_ collection: any EcoreValue, _ transform: (any EcoreValue) throws -> any EcoreValue) throws -> [any EcoreValue]

Parameters

collection

The collection to transform

transform

A closure that takes an element and returns a transformed value

Return Value

A new array containing the transformed elements

Discussion

The collect operation applies a transformation to each element, creating a new collection of the same size with the transformed elements. This is equivalent to the map operation in functional programming.

Examples

let numbers = [1, 2, 3] as [EInt]
let doubled = try collect(numbers) { ($0 as? EInt ?? 0) * 2 }
// Result: [2, 4, 6]