-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet.swift
80 lines (62 loc) · 2.24 KB
/
Set.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// All Contributions by Match Group
//
// Copyright © 2025 Tinder (Match Group, LLC)
//
// Licensed under the Match Group Modified 3-Clause BSD License.
// See https://github.com/Tinder/CollectionBuilders/blob/main/LICENSE for license information.
//
extension Set {
@resultBuilder
public enum Builder {
// swiftlint:disable:next nesting
public typealias Expression = Element
// swiftlint:disable:next nesting
public typealias Component = Set<Element>
// swiftlint:disable:next nesting
public typealias FinalResult = Set<Element>
public static func buildExpression(_ expression: Expression?) -> Component {
guard let expression: Expression
else { return [] }
return [expression]
}
public static func buildExpression(_ component: Component?) -> Component {
guard let component: Component
else { return [] }
return component
}
public static func buildBlock(_ components: Component...) -> Component {
components.reduce(into: Set()) { $0.formUnion($1) }
}
public static func buildOptional(_ component: Component?) -> Component {
component ?? []
}
public static func buildEither(first component: Component) -> Component {
component
}
public static func buildEither(second component: Component) -> Component {
component
}
public static func buildArray(_ components: [Component]) -> Component {
components.reduce(into: Set()) { $0.formUnion($1) }
}
public static func buildLimitedAvailability(_ component: Component) -> Component {
component
}
public static func buildFinalResult(_ component: Component) -> FinalResult {
component
}
}
public init(@Builder elements: () -> Self) {
self = elements()
}
public static func build(@Builder elements: () -> Self) -> Self {
elements()
}
public mutating func insert(@Builder elements: () -> Self) {
formUnion(elements())
}
public func inserting(@Builder elements: () -> Self) -> Self {
union(elements())
}
}