-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathEither.swift
322 lines (264 loc) · 6.92 KB
/
Either.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import Prelude
public enum Either<L, R> {
case left(L)
case right(R)
}
extension Either {
public func either<A>(_ l2a: (L) throws -> A, _ r2a: (R) -> A) rethrows -> A {
switch self {
case let .left(l):
return try l2a(l)
case let .right(r):
return r2a(r)
}
}
public var left: L? {
return either(Optional.some, const(.none))
}
public var right: R? {
return either(const(.none), Optional.some)
}
public var isLeft: Bool {
return either(const(true), const(false))
}
public var isRight: Bool {
return either(const(false), const(true))
}
}
public func either<A, B, C>(_ a2c: @escaping (A) -> C, _ b2c: @escaping (B) -> C) -> (Either<A, B>) -> C {
return { ab in
ab.either(a2c, b2c)
}
}
public func lefts<S: Sequence, L, R>(_ xs: S) -> [L] where S.Element == Either<L, R> {
return xs |> mapOptional { $0.left }
}
public func rights<S: Sequence, L, R>(_ xs: S) -> [R] where S.Element == Either<L, R> {
return xs |> mapOptional { $0.right }
}
public func note<L, R>(_ default: L) -> (R?) -> Either<L, R> {
return optional(.left(`default`)) <| Either.right
}
public func hush<L, R>(_ lr: Either<L, R>) -> R? {
return lr.either(const(.none), R?.some)
}
extension Either where L == Error {
public static func wrap<A>(_ f: @escaping (A) throws -> R) -> (A) -> Either {
return { a in
do {
return .right(try f(a))
} catch let error {
return .left(error)
}
}
}
public static func wrap(_ f: @escaping () throws -> R) -> Either {
do {
return .right(try f())
} catch let error {
return .left(error)
}
}
public func unwrap() throws -> R {
return try either({ throw $0 }, id)
}
}
extension Either where L: Error {
public func unwrap() throws -> R {
return try either({ throw $0 }, id)
}
}
public func unwrap<R>(_ either: Either<Error, R>) throws -> R {
return try either.unwrap()
}
public func unwrap<L: Error, R>(_ either: Either<L, R>) throws -> R {
return try either.unwrap()
}
// MARK: - Functor
extension Either {
public func map<A>(_ r2a: (R) -> A) -> Either<L, A> {
switch self {
case let .left(l):
return .left(l)
case let .right(r):
return .right(r2a(r))
}
}
public static func <¢> <A>(r2a: (R) -> A, lr: Either) -> Either<L, A> {
return lr.map(r2a)
}
}
public func map<A, B, C>(_ b2c: @escaping (B) -> C) -> (Either<A, B>) -> Either<A, C> {
return { ab in
b2c <¢> ab
}
}
// MARK: - Bifunctor
extension Either {
public func bimap<A, B>(_ l2a: (L) -> A, _ r2b: (R) -> B) -> Either<A, B> {
switch self {
case let .left(l):
return .left(l2a(l))
case let .right(r):
return .right(r2b(r))
}
}
}
public func bimap<A, B, C, D>(_ a2b: @escaping (A) -> B, _ c2d: @escaping (C) -> D)
-> (Either<A, C>)
-> Either<B, D> {
return { ac in
ac.bimap(a2b, c2d)
}
}
// MARK: - Apply
extension Either {
public func apply<A>(_ r2a: Either<L, (R) -> A>) -> Either<L, A> {
return r2a.flatMap(self.map)
}
public static func <*> <A>(r2a: Either<L, (R) -> A>, lr: Either<L, R>) -> Either<L, A> {
return lr.apply(r2a)
}
}
public func apply<A, B, C>(_ b2c: Either<A, (B) -> C>) -> (Either<A, B>) -> Either<A, C> {
return { ab in
b2c <*> ab
}
}
// MARK: - Applicative
public func pure<L, R>(_ r: R) -> Either<L, R> {
return .right(r)
}
// MARK: - Traversable
public func traverse<S, E, A, B>(
_ f: @escaping (A) -> Either<E, B>
)
-> (S)
-> Either<E, [B]>
where S: Sequence, S.Element == A {
return { xs in
var ys: [B] = []
for x in xs {
let y = f(x)
switch y {
case let .left(e):
return .left(e)
case let .right(y):
ys.append(y)
}
}
return .right(ys)
}
}
/// Returns first `left` value in array of `Either`'s, or an array of `right` values if there are no `left`s.
public func sequence<E, A>(_ xs: [Either<E, A>]) -> Either<E, [A]> {
return xs |> traverse(id)
}
// MARK: - Alt
extension Either: Alt {
public static func <|> (lhs: Either, rhs: @autoclosure @escaping () -> Either) -> Either {
switch lhs {
case .left:
return rhs()
case .right:
return lhs
}
}
}
// MARK: - Bind/Monad
extension Either {
public func flatMap<A>(_ r2a: (R) -> Either<L, A>) -> Either<L, A> {
return either(Either<L, A>.left, r2a)
}
}
public func flatMap <L, R, A>(_ r2a: @escaping (R) -> Either<L, A>) -> (Either<L, R>) -> Either<L, A> {
return { lr in
lr.flatMap(r2a)
}
}
public func >=> <E, A, B, C>(f: @escaping (A) -> Either<E, B>, g: @escaping (B) -> Either<E, C>) -> (A) -> Either<E, C> {
return f >>> flatMap(g)
}
// MARK: - Eq/Equatable
extension Either: Equatable where L: Equatable, R: Equatable {
public static func == (lhs: Either, rhs: Either) -> Bool {
switch (lhs, rhs) {
case let (.left(lhs), .left(rhs)):
return lhs == rhs
case let (.right(lhs), .right(rhs)):
return lhs == rhs
default:
return false
}
}
}
// MARK: - Ord/Comparable
extension Either: Comparable where L: Comparable, R: Comparable {
public static func < (lhs: Either, rhs: Either) -> Bool {
switch (lhs, rhs) {
case let (.left(lhs), .left(rhs)):
return lhs < rhs
case let (.right(lhs), .right(rhs)):
return lhs < rhs
case (.left, .right):
return true
case (.right, .left):
return false
}
}
}
// MARK: - Foldable/Sequence
extension Either where R: Sequence {
public func reduce<A>(_ a: A, _ f: @escaping (A, R.Element) -> A) -> A {
return self.map(Prelude.reduce(f) <| a).either(const(a), id)
}
}
public func foldMap<S: Sequence, M: Monoid, L>(_ f: @escaping (S.Element) -> M) -> (Either<L, S>) -> M {
return { xs in
xs.reduce(.empty) { accum, x in accum <> f(x) }
}
}
// MARK: - Semigroup
extension Either: Semigroup where R: Semigroup {
public static func <> (lhs: Either, rhs: Either) -> Either {
return curry(<>) <¢> lhs <*> rhs
}
}
// MARK: - NearSemiring
extension Either: NearSemiring where R: NearSemiring {
public static func + (lhs: Either, rhs: Either) -> Either {
return curry(+) <¢> lhs <*> rhs
}
public static func * (lhs: Either, rhs: Either) -> Either {
return curry(*) <¢> lhs <*> rhs
}
public static var zero: Either {
return .right(R.zero)
}
}
// MARK: - Semiring
extension Either: Semiring where R: Semiring {
public static var one: Either {
return .right(R.one)
}
}
// MARK: - Codable
extension Either: Decodable where L: Decodable, R: Decodable {
public init(from decoder: Decoder) throws {
do {
self = try .right(.init(from: decoder))
} catch {
self = try .left(.init(from: decoder))
}
}
}
extension Either: Encodable where L: Encodable, R: Encodable {
public func encode(to encoder: Encoder) throws {
switch self {
case let .left(l):
try l.encode(to: encoder)
case let .right(r):
try r.encode(to: encoder)
}
}
}