-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathComplexOperators.swift
154 lines (132 loc) · 4.74 KB
/
ComplexOperators.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// RUN: %target-typecheck-verify-swift
/// The function composition operator is the only user-defined operator that
/// operates on functions. That's why the exact precedence does not matter
/// right now.
infix operator ∘ : CompositionPrecedence
// The character is U+2218 RING OPERATOR.
//
// Confusables:
//
// U+00B0 DEGREE SIGN
// U+02DA RING ABOVE
// U+25CB WHITE CIRCLE
// U+25E6 WHITE BULLET
precedencegroup CompositionPrecedence {
associativity: left
higherThan: TernaryPrecedence
}
/// Compose functions.
///
/// (g ∘ f)(x) == g(f(x))
///
/// - Returns: a function that applies ``g`` to the result of applying ``f``
/// to the argument of the new function.
public func ∘<T, U, V>(g: @escaping (U) -> V, f: @escaping (T) -> U) -> ((T) -> V) {
return { g(f($0)) }
}
infix operator ∖ : AdditionPrecedence
infix operator ∖= : AssignmentPrecedence
infix operator ∪ : AdditionPrecedence
infix operator ∪= : AssignmentPrecedence
infix operator ∩ : MultiplicationPrecedence
infix operator ∩= : AssignmentPrecedence
infix operator ⨁ : AdditionPrecedence
infix operator ⨁= : AssignmentPrecedence
infix operator ∈ : ComparisonPrecedence
infix operator ∉ : ComparisonPrecedence
infix operator ⊂ : ComparisonPrecedence
infix operator ⊄ : ComparisonPrecedence
infix operator ⊆ : ComparisonPrecedence
infix operator ⊈ : ComparisonPrecedence
infix operator ⊃ : ComparisonPrecedence
infix operator ⊅ : ComparisonPrecedence
infix operator ⊇ : ComparisonPrecedence
infix operator ⊉ : ComparisonPrecedence
/// - Returns: The relative complement of `lhs` with respect to `rhs`.
public func ∖ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Set<T>
where S.Iterator.Element == T {
return lhs.subtracting(rhs)
}
/// Assigns the relative complement between `lhs` and `rhs` to `lhs`.
public func ∖= <T, S: Sequence>(lhs: inout Set<T>, rhs: S)
where S.Iterator.Element == T {
lhs.subtract(rhs)
}
/// - Returns: The union of `lhs` and `rhs`.
public func ∪ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Set<T>
where S.Iterator.Element == T {
return lhs.union(rhs)
}
/// Assigns the union of `lhs` and `rhs` to `lhs`.
public func ∪= <T, S: Sequence>(lhs: inout Set<T>, rhs: S)
where S.Iterator.Element == T {
lhs.formUnion(rhs)
}
/// - Returns: The intersection of `lhs` and `rhs`.
public func ∩ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Set<T>
where S.Iterator.Element == T {
return lhs.intersection(rhs)
}
/// Assigns the intersection of `lhs` and `rhs` to `lhs`.
public func ∩= <T, S: Sequence>(lhs: inout Set<T>, rhs: S)
where S.Iterator.Element == T {
lhs.formIntersection(rhs)
}
/// - Returns: A set with elements in `lhs` or `rhs` but not in both.
public func ⨁ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Set<T>
where S.Iterator.Element == T {
return lhs.symmetricDifference(rhs)
}
/// Assigns to `lhs` the set with elements in `lhs` or `rhs` but not in both.
public func ⨁= <T, S: Sequence>(lhs: inout Set<T>, rhs: S)
where S.Iterator.Element == T {
lhs.formSymmetricDifference(rhs)
}
/// - Returns: True if `x` is in the set.
public func ∈ <T>(x: T, rhs: Set<T>) -> Bool {
return rhs.contains(x)
}
/// - Returns: True if `x` is not in the set.
public func ∉ <T>(x: T, rhs: Set<T>) -> Bool {
return !rhs.contains(x)
}
/// - Returns: True if `lhs` is a strict subset of `rhs`.
public func ⊂ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Bool
where S.Iterator.Element == T {
return lhs.isStrictSubset(of: rhs)
}
/// - Returns: True if `lhs` is not a strict subset of `rhs`.
public func ⊄ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Bool
where S.Iterator.Element == T {
return !lhs.isStrictSubset(of: rhs)
}
/// - Returns: True if `lhs` is a subset of `rhs`.
public func ⊆ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Bool
where S.Iterator.Element == T {
return lhs.isSubset(of: rhs)
}
/// - Returns: True if `lhs` is not a subset of `rhs`.
public func ⊈ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Bool
where S.Iterator.Element == T {
return !lhs.isSubset(of: rhs)
}
/// - Returns: True if `lhs` is a strict superset of `rhs`.
public func ⊃ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Bool
where S.Iterator.Element == T {
return lhs.isStrictSuperset(of: rhs)
}
/// - Returns: True if `lhs` is not a strict superset of `rhs`.
public func ⊅ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Bool
where S.Iterator.Element == T {
return !lhs.isStrictSuperset(of: rhs)
}
/// - Returns: True if `lhs` is a superset of `rhs`.
public func ⊇ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Bool
where S.Iterator.Element == T {
return lhs.isSuperset(of: rhs)
}
/// - Returns: True if `lhs` is not a superset of `rhs`.
public func ⊉ <T, S: Sequence>(lhs: Set<T>, rhs: S) -> Bool
where S.Iterator.Element == T {
return !lhs.isSuperset(of: rhs)
}