Documentation Language: Swift

Function

one(_:_:)

Tests whether exactly one element in the collection satisfies the given predicate.

func one(_ collection: any EcoreValue, _ predicate: (any EcoreValue) throws -> Bool) throws -> Bool

Parameters

collection

The collection to test

predicate

A closure that takes an element and returns a Boolean value

Return Value

true if exactly one element satisfies the predicate, false otherwise

Discussion

The one operation returns true if exactly one element in the collection satisfies the predicate. If zero elements or more than one element satisfies the predicate, it returns false.

Examples

let numbers = [1, 2, 3, 4] as [EInt]
let exactlyOneEven = try one(numbers) { ($0 as? EInt ?? 0) == 3 }
// Result: true